By train your Pokemon in gyms owned by a team you joining to, you can gain XP and increase the gym's prestige. As the gym gets its prestige increased, it levels up and can have more defenders.
Video
Guide
1. Gym's main menu
Once you get to friendly gyms, you can see the gym's level, prestige, and defending Pokemons. By checking those details of the gym, you can see that which Pokemon is the best for training in the gym.
2. See defenders
Dots by the crown-shaped mark indicates the number of defenders in the gym. How many Pokemon can be located in a gym is associated with gym's level. By swiping right or left, you can see what Pokemon is defending.
The order of defenders is from lower CP to hihger CP. For example, the gym in the video has the following four defenders.
Pidgeot, 274CP
Jolteon, 629CP
Vaporeon, 784
Flareon, 921CP
3. XP gained and prestige increased after training
After gym training, you can gain XP and increase the gym's prestige. You can get those even if you lose in the training.
3 things you need to know
Only one Pokemon can be used
Unlike gym battles in rival gyms, you can select only one Pokemon for training. So it is not easy to beat all the defenders, but you can gain more XP.
Pokemon is not fainted
Pokemon which lose battles of training is not fainted. Therefore, you do not prepare Revive items even if Pokemon get injured.
Gained XP and the value of prestige vary by the CP of Pokemon
If you use stronger Pokemon with higher CP, gained XP is lower and prestige is not increased much. On the other hand, if your Pokemon has lower CP, you can gain more XP and increase prestige higher.
To gain XP and increase prestige faster, you may want to use Pokemon with lower CP. To win training battle with such Pokemon, type effectiveness is very important.
The result of training: Pokemon with CP nearly twice as much as B
The result of training: Pokemon with lower CP than defenders
The result of training: Pokemon with CP nearly same to defenders