If you click on a friendly gym you can click the train button in the bottom right, it looks like a boxing glove. It's in the same position that the battle button is for an enemy gym.
You will then be able to choose a single Pokemon to go up against all the Pokemon in the gym. However you don't need to beat all of them, you will gain gym prestige for each Pokemon that you beat. You will still gain prestige even if you retreat from battle after defeating a Pokemon. Retreating is good to reduce your damage taken on your Pokemon if you think you aren't going to defeat the next one. You Pokemon can not faint from training, it's health will be reduced to a minimum of one before training automatically ends.
You gain 500 prestige for defeating each Pokemon with the same CP score. So if the gym pokemon is 300CP and you defeat it with a 300CP Pokemon you will get 500 prestige for the gym. If you defeat it with a lower CP Pokemon, you will get more and if you defeat it with a higher CP Pokemon you will get less prestige.
Thus I believe that the best way to train a gym is to look at the first Pokemon that you will be training against (the lowest CP gym Pokemon) and to counter that one, in both defense and attack (so their attacks are not very effective against you and your attacks are super effective against them) with a lower CP Pokemon. For example if they have a 500 CP water type Pokemon, you can use like a 300 CP grass type Pokemon to defeat it, then retreat from battle when you are facing the next Pokemon, and you will gain lots of prestige, approximately 600-800. Conversely if you defeat a 500 CP with a 1000CP pokemon you will only get about 100 prestige.
You also gain 10% of the prestige you got as xp.
You can also gain 2k prestige for placing a Pokemon in the gym. This is why level 2 and 3 gyms are very commong. When a gym is white, it will have 0/2000 prestige. You place a Pokemon in there, it gains 2k prestige and levels up to level 2 with 2000/4000 prestige. Which means you can place another Pokemon in it to level it up to level 3.
The most likely cause of this is that all of the gym's slots are full, which means you will not be able to add another Pokémon until the gym is upgraded. Gyms can hold a number of Pokémon equal to the gym's level, so the gym in question is likely only level one, and its slot is already full. To increase a gym's level, you must win friendly "battles" (training) against the gym using only one Pokémon (as opposed to the usual six). Every time you beat a Pokémon at that gym, its prestige will raise, eventually leveling it up and adding another slot. At this point, you should be able to add a new Pokémon to the gym.
As Riley mentioned, the button will appear in the bottom left corner of the gym when there is an empty slot, and any Pokémon that you wish to place in that gym must be full health.
Best Answer
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.