How do gyms work in general? Do my Pokemon battle others there? How are the stats taken into consideration if they do battle?
If I leave my Pokemon in a gym that I will not be returning to, can I get the Pokemon back?
Freekvd's answer covers the basics of gyms, but I think leaves out some important details with regards to Gym mechanics, I have tried to be more thorough in this answer.
Gyms are the only locations (currently) where Pokémon battles can occur. When you are battling at a Pokémon Gym, you are either battling an opposing team, using a full roster of six Pokémon that you have selected, or battling ("training against") your own team, using a single Pokémon that you have selected.
Combat itself is relatively straightforward - single tap repeatedly to perform your Pokémon's basic attack and to charge up your secondary attack, and then long-tap (hold down) to use your secondary attack once it is charged. You can also swipe to the left or right to dodge the enemy's attack, although this is a bit finnicky and the timing can be a bit tricky to get right. Beyond this, the results of a Pokémon battle differ depending on the two types of gyms:
These are gyms that are currently controlled by players who are not on the same team as you (i.e. Instinct (Yellow), Mystic (Blue), or Valor (Red)). When you engage in battle against these gyms, you must pre-select a roster of six Pokémon to fight with. Then you will fight the Pokémon in the gym one by one from weakest to strongest until you have beaten them all (or not). For every enemy Pokémon that you defeat, the prestige of that gym will be reduced. If the prestige is reduced enough, that gym will lose a level and the weakest Pokémon will be kicked out and returned to its trainer. If the gym loses enough prestige and falls below level one, it will turn white (neutral) allowing you to install a Pokémon of your own and claim this gym for your team.
These are Gyms that are currently controlled by players who are on the same team as you. When you battle at these gyms, you must select a single Pokémon to fight with, and then combat proceeds as normal against the Pokémon in that gym. Winning these will result in an XP reward, and the prestige of that gym will be increased; however, any Pokémon that you defeat will remain in the gym, unlike battles against enemy gyms. You can also install your own Pokémon in a friendly gym provided there is enough space, which is based on the gym's prestige level (e.g. Prestige 2 gyms can hold two Pokémon). Installing your own Pokémon in a gym can be beneficial because it allows you to collect a reward of Pokécoins and Stardust once every ~20 hours for each Pokémon you have installed in a gym. The maximum any player can have in any singular gym is one Pokémon.
If you get to a gym that is free, you can put one of your pokemon in there. If the gym's occupied by a player from another color, you will have to beat that player first.
If you put your pokemon inside a gym and it gets beaten, it will be returned to you.
When you find a allied gym fight it. It will increase the prestige of the gym causing more players to be able to hold it.
A group of friends and I always train at gyms after we caught them till its at least prestige 5 (if you beat a gym and take it over its 3) so players have a much tougher time dealing with them.
Be a team player and train your own gyms. Having a stable is better than capturing and losing gyms
I call them XP boosters. Once you have strong Pokemon (CP above 1200), find gyms of the two other teams. You can easily beat it using only 2 or more Pokemon. Battles usually last 1 minute or so. Once you beat them you get XP ranging from 150-500.
I have a Vaporeon with CP of 1210. It is strong enough it take out 4 Pokemon max and min 2. I have personally done this to increase my XP and move to higher levels.
And one more thing never place you Pokemon after you take out the gym. Just leave it move on and find another.