When you fight in a gym you’re never actually fighting another player: the defending Pokémon are controlled by an AI, as are yours later, when they’re resident in the gym and defending. Once a team has at least one Pokémon in the gym, it will change to the corresponding colour. The higher the level of prestige a gym has, the more Pokémon it can hold. You fight until you win or the six Pokémon you’re allowed to take into battle with you are defeated. They are the battle arenas of the Pokémon world, but until your trainer reaches level five, you can’t even enter a gym.
Source: The Guardian July 25, 2016 06:00 UTC