Once you reach level 5 in Pokemon Go you would gain access to the Pokemon GYM feature. There are various ways you can interact with a GYM.
An unclaimed GYM
It is highly unlikely at this point of time that you might come across a GYM that has not been claimed yet. You can identify an unclaimed GYM by its color, a GYM that has a white theme to it is unclaimed. All you need to do it get into its range and drop off one of your Pokemon and then the GYM color changes to your team’s color and you gain XP for that.
Attack an enemy team’s GYM
Most often you would find that a GYM is owned by a rival team. The idea is to explore the GYM by clicking on it and browsing through the Pokemon that are protecting the GYM. That way you can chose your Pokemon wisely for the GYM battle. There are two scenarios here either you defeat all the Pokemon’s defending the GYM or you lose the battle. When you win the battle you get to claim the GYM and put your Pokemon there to defend it and you gain XP for the victory. But, if you lose the battle you still get XP if you defeat at least one Pokemon at the GYM and you also lower the reputation of the GYM which instead adversely affects the number of Pokemon that the GYM can hold. Another tip would be to revive your Pokemon and go back to the GYM and defeat the Pokemon at the GYM before the trainers replace them.
Defending your team’s GYM
If you come across a GYM that has been claimed by your team you can help your team defend it. The objective of claiming a GYM is to defend it and also help it gain prestige so that it can house more Pokemon. The GYM generates coins and XP for the team members who house their Pokemon to protect that GYM. Browse through the GYM that your team own by walking into its range. If you find a spot in the GYM that’s free, you can put your Pokemon there. Once you have your Pokemon defending a GYM then you can walk up to a PokeStop and you would notice a shield at the top right of your screen. That shield indicates the number of Pokemon you have protecting a GYM. Once the shield is ready to be claimed you can click on it and you get Pokemon coins and Stardust.