There will be 2 types of gyms that you come across while playing Pokemon Go. Gyms that your team owns and gyms that other teams own. This means that there are 2 possible ways to join a gym, heres how to do it.
When you find a gym that your team owns you may be able to join it immediately. A gym can only hold a certain number of trainers. The number is determined by the level of the gym. For example a level 3 gym can hold 3 trainers. If you find a level 3 gym and there are only 2 trainers, you should be able to click the button on the bottom left to join the gym immediately. If there are 3 trainers there already you will not be able to join this gym yet. When you view the gym, take note of the prestige score on the top right. It might say something like 6000/8000. This means that the gym needs 2000 more prestige before it levels up and unlocks a spot for a new trainer. There will be a button on the bottom right of the main screen that lets you use 1 Pokemon to battle the other trainers. Keep fighting the trainers until the gym levels up. Once it levels up you will be able to join the gym.
If you find a gym that another team owns, it can be a little easier to join, which is surprising. You can trigger a battle which lets you use 6 of your best pokemon to try and take out all of the trainers at the gym. Don’t worry if you fail, for each trainer you defeat the gym will loose some prestige. Once the gym reaches 0, you can take over the gym as the first leader. It’s worth knowing that even if you defeat all trainers first time around, it doesn’t mean the gym is defeated. It will take away a lot of the gyms prestige if you do defeat everyone, but you may need to do it more than once before they loose everything.