The rules text from Making an Attack in the SRD 5.1 (the 2014 rules), with the rules around it and the 2024 (SRD 5.2) text that goes with it.
In a fight, everyone is constantly watching for a chance to strike an enemy who is fleeing or passing by. Such a strike is called an opportunity attack.
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.
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 a foe's reach or if gravity causes you to fall past an enemy.
Disengage (from Actions in Combat)
If you take the Disengage action, your movement doesn't provoke opportunity attacks for the rest of the turn.
Reactions (from The Order of Combat)
Certain special abilities, spells, and situations allow you to take a special action called a reaction. A reaction is an instant response to a trigger of some kind, which can occur on your turn or on someone else's. The opportunity attack is the most common type of reaction.
When you take a reaction, you can't take another one until the start of your next turn. If the reaction interrupts another creature's turn, that creature can continue its turn right after the reaction.
Reach weapons
This weapon adds 5 feet to your reach when you attack with it, as well as when determining your reach for opportunity attacks with it. The reach weapons: Glaive, Halberd, Lance, Pike, Whip.
In the 2024 rules (SRD 5.2): “A Reach weapon adds 5 feet to your reach when you attack with it, as well as when determining your reach for Opportunity Attacks with it.”
How opportunity attacks work
When a hostile creature you can see moves out of your reach, you can use your reaction to make one melee attack against it. The attack happens just before it leaves, so if you drop it to 0 hit points, it never gets away. That’s the whole rule: leaving your reach provokes, nothing else does.
Moving around you while staying within your reach doesn’t provoke, and neither does walking up to you. Neither does being moved by something else: a creature shoved, thrown by a Thunderwave or falling past you isn’t using its own movement. Teleporting, as with Misty Step, never provokes, and a creature that takes the Disengage action doesn’t provoke for the rest of its turn.
You get one reaction per round, and it comes back at the start of your turn. Spend it on an opportunity attack and you can’t also cast Shield or Counterspell until then, and the reverse is true too.
Using opportunity attacks well
Your reach is your zone. With a reach weapon such as a glaive or whip, your reach is 10 feet, so enemies have to go twice as far to get past you, and they provoke when they step from 10 feet away to 15.
Stand in doorways and corridors. An opportunity attack is mostly a threat. Enemies who have to walk past the fighter to reach the wizard either take a hit or spend their action on Disengage.
Get away cleanly. Rogues can Disengage as a bonus action with Cunning Action, and goblins do the same with Nimble Escape. Everyone else can spend their action on Disengage, teleport, or hit the enemy with Shocking Grasp first, which stops it taking reactions until its next turn.
Don’t forget the monsters. A cleric running past the ogre to reach a downed ally, a wizard backing away from an orc: the DM should take these attacks too, which is what makes positioning matter.
What changed in 2024
The core rule is the same in the 2024 Player’s Handbook: a creature provokes when it leaves your reach using its own movement, action or reaction, you spend your reaction on one melee attack, and Disengage and teleportation avoid it. Boots of Speed still give opportunity attacks against the wearer disadvantage.
Common questions
Does moving past an enemy provoke an opportunity attack?
Only if you leave its reach. You can walk all the way around an enemy, or past it, without provoking as long as you stay within its reach the whole time; the attack comes when you step out.
Does teleporting or being pushed provoke an opportunity attack?
No. Teleporting never provokes, and neither does being moved without using your own movement, action or reaction, such as being shoved, thrown by a spell or falling.
Can I make an opportunity attack with a ranged weapon or a spell?
Not with a ranged weapon: it’s one melee attack. Casting a spell instead needs the War Caster feat from the Player’s Handbook, which lets you cast a spell with a casting time of 1 action that targets only that creature.
How many opportunity attacks can I make in a round?
One, because it uses your reaction and you get one reaction per round. Even a fighter with Extra Attack makes only a single attack with it.
Does standing up from prone provoke an opportunity attack?
No. Standing up costs movement but doesn’t take you out of anyone’s reach. Only moving out of a hostile creature’s reach provokes.
Can I use an unarmed strike for an opportunity attack?
Yes. An unarmed strike is a melee attack, so it works. Monks rely on it.