I put my Pokemon in a gym. Then, the gym was taken and held by several different teams, but I did not get my Pokemon back. It is still showing it's assigned to the gym. What can I do to get it back? It was my highest CP Pokemon.
Just wait. It will take a little while for the game to check Gym status and establish that your Pokémon was defeated and return it to you.
In my experience, gyms tend to update the Pokemon inside of them to be returned every 5 to 15 or so minutes, regardless of how many times it was recaptured. As @NiettheDarkAbsol said, wait a little bit of time then check back.