Beating 3 Pokemon in a level 3 gym (not your team) will reduce the gym to level 2 or 1 with a corresponding number of Pokemon guarding it. Once you bring it down to zero, or neutral, you will be able to claim the gym for your team and place a Pokemon inside