Attacks of opportunity are only allowed to be taken when an enemy leaves your threat range.
You can make an opportunity attack when a hostile creature that you can see moves out of your reach. To make the opportunity attack, you use your reaction to make one melee attack against the provoking creature, the attack occurs right before the creature leaves your reach.
So side-stepping around an enemy will never provoke an opportunity attack from that creature. Also when fighting a creature using a melee weapon with a reach of 10 feet (lances, halberds, etc), or when fighting a creature that naturally has longer reach, a creature stepping back won't proc an opportunity attack until leaving the hostile creature's reach. As a result when facing an opponent armed with a pole arm, a character can step back without provoking an attack of opportunity as long as they remain within the reach of the weapon.
A character can avoid procing an opportunity attack through various means.
You can avoid provoking an opportunity attack by taking the disengage action. You also don't provoke an opportunity attack when you teleport or when someone or something moves you without using your movement, action, or reaction. For example you don't provoke an opportunity attack if an explosion hurls you out of an opponent's reach...
Shoving a creature or using spells like thunderwave or gust of wind won't allow your allies to make opportunity attacks against the opponent, however if a creature fails checks against dissonant whispers or the fear spell, their resulting movement can provoke opportunity attacks.
There are various ways of adjusting when a character can take an opportunity attack and in what way. Most commonly feats (if your campaign allows them).
Polearm Master feat;
While you are wielding a glaive, halberd, pike, or quarterstaff, other creatures provoke an opportunity attack when they enter the reach you have with that weapon.
When you hit a creature with an opportunity attack its speed becomes 0 for the rest of the turn. Creatures provoke opportunity attacks from you even if they take the disengage action before leaving your reach. When a creature within 5 feet of you makes an attack against a target other than you (and that target doesn't have this feat) you can use your reaction to make a melee weapon attack against the attacking creature.
The last one is not technically an opportunity attack and does not proc the other affects of this feat, but I decided to include it for completeness.
When a hostile creature's movement provokes an opportunity from you, you can use your reaction to cast a spell on the creature, rather than making an opportunity attack. The spell must have a casting time of 1 action and must target only that creature.
These should cover the most common situations where opportunity attacks come up. If I missed any go ahead and mention them in the comments.
This doesn't count as an opportunity attack but I think it is worth including here for completeness and disambiguation.
When a creature within 5 feet of you casts a spell you can use your reaction to make a melee attack against that creature.
Although this isn't technically an opportunity attack I thought it should be included for completeness