Only moving out of an enemy's reach using your movement/action/reaction provokes opportunity attacks
From the Basic Rules, p74:
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 interrupts the provoking creature’s
movement, occurring right before the creature
leaves your reach.
No other situations are listed. Note that unlike previous editions, only leaving an enemy's reach provokes; moving from one position in their reach to another does not provoke. Being forced out of a creature's reach also doesn't provoke, as when pushed or falling.
Ranged attacks do not provoke; from the Basic Rules, p73:
Aiming a ranged attack is more difficult when a foe
is next to you. When you make a ranged attack with
a weapon, a spell, or some other means, you have
disadvantage on the attack roll if you are within 5 feet
of a hostile creature who can see you and who isn’t
incapacitated.
Ranged attacks have disadvantage when in melee, but do not provoke.
Spells do not provoke or suffer any other penalties for being cast in melee (other than those that require a ranged attack, which suffer disadvantage as mentioned above).
Some feats may expand how and when an opportunity attack is triggered. In these cases, the specific wording of the feat will override the general rule.