For example, documentaries will say something along the lines of "xxxx, realizing that the situation had become critical, disengaged his soldiers and retreated to xxxx" or "the army then retreated to xxxx where it took up defensive positions"
Now, considering battle was done via melee combat, how would an army effectively retreat without being slaughtered in the process? Did generals leave certain units behind to soak up the enemy advanced as the bulk of their forces retreated?
I should point out that this isn't just soecific to the middle ages, but any period where battle was done with swords, bows, etc.
For one thing, battles that ended in routs naturally meant that the losing side was not able to disengage effectively or retreat in good order, whether it was a lack of discipline, enemy being too quick or too close to get away, no where to run, etc so those were all the cases where the enemy did catch the losing side, which often happened, since disengaging from melee in good order is incredibly hard.
Being able to disengage and retreat in good order was the sign of a professional, disciplined, and loyal army because it would invariably cause casualties as men were cut off, left behind, parts of the line losing coherency, etc. It also involved many men receiving and adhering to orders in the heat of battle.
In short, it was incredibly hard and it was the sign of a robust army.
Rearguard actions were common. This is where 'blocker' forces are used to essentially buy the rest of the retreating army time to fall back and regroup. Sometimes these rearguard actions were essentially suicidal in nature, where the assumption was those fighting in the delaying action were doomed, but not always.
There were many examples of successful rearguard actions that didn't involve the delaying force to sacrifice their own lives, sometimes even turning the tables on their pursuers.
But having a rearguard involves manpower, willing soldiers, and supreme discipline/training on the part of those staying behind to fight, knowing they are the last ones to leave.
In many cases, the pursuers are just as exhausted as the retreating forces and following at a quick pace is strategically unwise or just plain impossible. Many commanders were and are wary of following retreating forces too closely, because military history is filled with overeager armies being crushed by the ruse of a carefully faked retreat.
Melee battles were arduous work and perhaps more importantly, moving through the field was mostly done on foot, where one's speed was directly affected by exhaustion.
Retreating armies also have tactical options that are more appealing for the desperate more than the successful attackers.
For example, they could abandon the wounded, their slower supply trains, and let slow camp followers/soldiers fend for themselves if they risked the retreat. The pursuers would be harder pressed to abandon these things since they have the breathing room to justify treating the wounded, bringing up supply, etc.
Also, pursuing too quickly after a big battle would mean risking the army in a very uncohesive state. Victorious army or not, battles are chaotic and both armies would likely not be very well organized. To move efficiently (and safely), armies need to regroup after battles.
Finally, in certain contexts, the retreating army is going back to familiar ground, or areas where they have reserves, allies, garrisons, etc
If the retreating army is falling back into 'safe' ground, it's doubly risky for the pursuing army to go in unprepared, especially if they don't know what's waiting for them while the retreating army does.
Speaking about terrain, sometimes terrain makes it difficult to pursue effectively, like thick forests, rough hillsides, rivers, etc.
Of course, this isn't applicable in every situation but it's something to consider.
With all these difficulties, how did pursuing armies ever catch their prey?
Well, cavalry played a big part, when they were in play. Reserve units of light cavalry or light infantry specifically intended to hunt down retreating enemies existed, for good reason.
Other times, terrain was simply not conducive to retreating.
And of course, tactically cutting off routes for retreat, like surrounding the enemy.
This is a very general overview, since you didn't mention any specific time periods or places. This can and does change drastically depending on the time period, armies involved, place, specific situation and circumstance, etc. But it's a start!
If you were routed, yeah, the enemy just swarmed up your back. In Classical Greece, an army in rout threw down their heavy hoplite shields and helmets, certainly their big spears, so they could outrun pursuit, which kept their weapons and armour. This led to the winning side collecting the abandoned stuff to build a trophy as a sign of victory and a place for thank-offerings.
But many armies have managed to retreat in good order, carefully backing out of a situation. The still-classic text of a retreat through mountainous country is Xenophon's Anabasis which is oft translated as "The Going-Down" but could be called "The Retreat."
By picking out the Middle Ages, though, you have opened the can of worms labelled "Medievals Didn't Think Like Us."
They had their own military science, and through much of the Middle Ages that included the doctrine of the "Golden Bridge." (Hans Delbrück, History of the Art od War) this simply meant that they believed you should never trap your enemy completely, but if necessary build him a bridge of gold to retreat across. This meant they avoided battles to the last man. They thought a trapped enemy would fight above themselves, making the victory too costly, and possibly reversing who won.
Nuts, right? But this is what they believed, because they lived in a world without nations, without total warfare, where you more usually captured and ransomed your enemy - who might be a relative of yours or a friend, because they married by social level not nationality. So not just technology but culture gave them a different view of warfare.
The Golden Bridge idea dissappeared toward the Renaissance. But while that made retreats harder, they could still be conducted. You probably need to ask /r/militaryhistory about the details of how they disengage and retreat.