In the sixteenth century, when an army attacked a walled city or fortress, they would advance by digging a series of trenches for protection until they were close enough to storm the walls.
If there was a successful counter-attack, the invaders would retreat by attempting to hold each trench in the reverse order from which they had advanced until they might find themselves fighting from the “last ditch.”
If they failed to hold that one, the battle was lost.
That’s how we got the expression last-ditch stand.