You don’t need to have a Pokemon in a gym, before you battle. In fact, you will normally have to battle it first, before you will be able to put your own in there. There needs to be a space for it. You generally have to attack it and for every pokemon you defeat, you will gain Prestige points. Get enough Prestige points to get the gym up to the next level, and a space will open for you. Then when you put your pokemon in, it will appear in the lineup, based on it’s CP. If it’s the highest, it will go straight to the number 1 spot.
You only get to choose one Pokemon to attack the gym with, not 6 like in a battle. Once he is defeated, you have to re-fill his HP using potions and go again.