What happens when I team swap while I have Pokemon inside a gym?
If you have Pokemon in any gyms when you change teams, they will stay in the gyms. You will not be able to fight your own pokemon to bring them home, and you will not get any Pokecoins when they are returned to you. The gym will stay the original color, even if you are the only one in the gym.
My advice: Stop putting pokemon into gyms and wait for them all to come home, or else you run the risk of losing Pokecoins they could earn for you.
Source: http://www.pokemon.com/us/pokemon-news/switch-teams-in-pokemon-go/