In the wild, when startled or alarmed, animals survive through the flight-or-fight response. This is the same response that kicks in when someone jumps out from the dark and scares you: do you throw up your hands to fight, or do you engage your feet and take flight? Humans, who have been called the ultimate predators, often fight. Horses, who are in almost all situations prey (except for fights between horses), take flight.
By natural selection, horses who could sleep standing up, wake up, and run away from predators faster were the ones more likely to survive and pass on their genes. Put another way: when you’re a large herbivore and a carnivore with a rumbling stomach looks your way, you’re better off if you can move at a moment’s notice.
So that’s why horses sleep standing up, but how do they do it? The answer is called a “stay apparatus”, which is a unique adaptation of the musculoskeletal system of the horse that allows the animal to lock limbs in position so that very little muscle function is required to remain standing.