I have been walking around and found a level 5 gym that looks hard to defeat, but my pokemon are only in level 2 and 3 gyms. If anyone knows how to get a gym to have higher prestige level it would be very helpful.
If you click on a friendly gym you can click the train button in the bottom right, it looks like a boxing glove. It's in the same position that the battle button is for an enemy gym.
You will then be able to choose a single Pokemon to go up against all the Pokemon in the gym. However you don't need to beat all of them, you will gain gym prestige for each Pokemon that you beat. You will still gain prestige even if you retreat from battle after defeating a Pokemon. Retreating is good to reduce your damage taken on your Pokemon if you think you aren't going to defeat the next one. You Pokemon can not faint from training, it's health will be reduced to a minimum of one before training automatically ends.
You gain 500 prestige for defeating each Pokemon with the same CP score. So if the gym pokemon is 300CP and you defeat it with a 300CP Pokemon you will get 500 prestige for the gym. If you defeat it with a lower CP Pokemon, you will get more and if you defeat it with a higher CP Pokemon you will get less prestige.
Thus I believe that the best way to train a gym is to look at the first Pokemon that you will be training against (the lowest CP gym Pokemon) and to counter that one, in both defense and attack (so their attacks are not very effective against you and your attacks are super effective against them) with a lower CP Pokemon. For example if they have a 500 CP water type Pokemon, you can use like a 300 CP grass type Pokemon to defeat it, then retreat from battle when you are facing the next Pokemon, and you will gain lots of prestige, approximately 600-800. Conversely if you defeat a 500 CP with a 1000CP pokemon you will only get about 100 prestige.
You also gain 10% of the prestige you got as xp.
You can also gain 2k prestige for placing a Pokemon in the gym. This is why level 2 and 3 gyms are very commong. When a gym is white, it will have 0/2000 prestige. You place a Pokemon in there, it gains 2k prestige and levels up to level 2 with 2000/4000 prestige. Which means you can place another Pokemon in it to level it up to level 3.
In the top left corner you'll see a fraction. The gym prestiges to a new level ones the numerator reaches the denominator. To gain prestige, you must train at gyms belonging to your own team. Note that as of a recent patch, Pokemon will faint while doing this.
Train against your gym with your own Pokemon. I will increase the prestige.
Sorry for the short answer but I lack time right now. I shall update later if a better answer is not posted.