How can I put my pokemon in an allied gym in Pokemon Go?

If a gym belongs to your own team it can hold one defending Pokemon for each gym level. So a level 1 gym can hold 1 defender, level 2 can hold 2 defenders, etc. The spots are basically on a first come first serve basis. To level up the gym you can do "training" battles with the Pokemon that are in the gym by clicking the boxing glove icon on the bottom right.