When does your pokemon get CP at a gym?
Pokemon don't gain CP at a gym. Successfully training at a gym raises only that gym's "prestige," and consequentially, the gym's level.
The only way to increase the CP of your Pokemon is by using Stardust to power them up or evolve them.
The purpose of a gym is to put a Pokemon on it to defend it which can earn you 10 gold coins each (max 100) per 21 hours. You can collect them in the shop at the right top. This can be done straight after or you can wait.
One point to clarify from Bluerex, is that you can get the Defenders reward (10 gold coins and 500 stardust) whether you are the gym leader or not. So far there are no extra benefits from being the gym leader except for showing off your high level Pokemon (compared to others in that gym). I guess your Pokemon in the gym has a little more security as the lower level Pokemon need to be removed before your's does.