This question is really more of a tactics and plotting question than a rules question. In D&D 5e, a creature can avoid opportunity attacks when running away by using the Disengage action (although some features allow opportunity attacks in spite of this).
While DMing combat encounters I have found that by the time I think a creature might want to flee, there is usually a really strong chance that the players will drop the creature anyway. But fleeing earlier seems absurd or anticlimactic, like the creature runs at the first sign of blood. It's widely discussed in optimization that D&D 5e combats tend to finish in about 3 rounds. If the characters are knocking out ⅓ or more of a creatures hit points each round, the combat looks like this:
- Creature reduced to ⅔ of their maximum hit points.
- Creature reduced to ⅓ of their maximum hit points. Creature decides to flee.
- Creature flees using Disengage or Dash:
- Creature takes Disengage action and runs 30 feet. Characters run 30 feet and kill the creature.
- Creature takes the Dash action. Characters get opportunity attacks and kill the creature.
Modules are often written with notes on when creatures will flee. For a group of lower CR creatures (relative to the party), a module might say "If more than half their number are killed the remaining orcs will run away." It's usually not problematic if a handful of the fleeing orcs die. For a single creature that is a medium or hard encounter, a module might say "If the frost giant loses more than half their hit points, they will run away." If the players do enough damage to the fleeing frost giant to kill it, whether that is a problem depends on the frost giant's role in the plot.
The problem really comes when it is a single creature who needs to survive for plot reasons.
There are several features that could allow a creature to flee safely. Various teleportation abilities (teleport, but also short range teleports like misty step or dimension door) will allow the creature to completely vanish or at least allow movement to a safe distance from which to run away. Special movement rates such as flying, swimming, or climbing can help, so long as the characters don't also have that movement ability. The problem primarily would arise for a terrestrial, non-magical creature who needs to run away on foot. (But, certainly, if some or most of the party have a flying or swimming speed, as may be the case with high-level parties or unusual character races, the problem transfers.)
On the one hand, the obvious solution is "Give the creature a feature that allows them to flee successfully." This could be a potion of flying if they can't usually fly, say that they have the equivalent of the rogue's Cunning Action feature so they can Disengage and Dash in the same turn, give them a magical whatsit that allows them to dimension door, etc. The problem with this solution is really character annoyance at seemingly arbitrary abilities that exist transparently to allow creatures to escape. Giving the creature allies or minions who could help with the getaway may also be plot-breaking, if, for example, the adversary is supposed to be a loner.
Basically, how do you save the goblin king when the goblin king is just a goblin with an unusually large pool of hit points?