Friendly Gyms (your color)
If a friendly gym is already owned, you'll get options to either train at it or, if there's an open slot, add a Pokémon . The number of slots for Pokemon is determined by the Gym's level. To train, you select one Pokémon . And attempt to fight through all defenders. The defenders won't actually be harmed, and your Pokemon won't faint if you lose (though it will take damage). For this, you want to choose a Pokemon you can win with (you and the gym get nothing if you lose), but the closer in CP to the highest Pokémon in the Gym, the better- especially if you can win with lower. An ideal Pokémon for this task is one of the super effective type of the strongest Pokémon in the gym, slightly below that Pokémon in CP.
If you defeat the Gym, you'll gain XP and the Gym gets Prestige. There's a 5 min cooldown on training. With enough Prestige it will level up, thus unlocking a new slot and you can then choose to add a new Pokémon to defend it, and gain access to the Daily Bonus (go to the shop and click the shield in the top right). If the Pokémon you add now has the highest CP of the Gym you become its leader (and the last to be fought when attacked)