I went to a friendly gym and battled the lowest player and won multiple times, enough to up the gym level and leave my Ratticate there. However his CP is still 313. I thought the purpose of friendly fighting was to increase CP for your pokemon. What am I missing?
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.