Why, when we twist and turn in our sleep so much, it is rare that we fall out of bed?
Even when you're asleep, your brain is still aware of its surroundings to some extent. It will have remembered how big your bed is and how far your body can safely turn. Should you get too close to the edge, your brain will tell your muscles, and you'll roll backward.
Be warned, though: Your brain will often lose its bearings if you've been drinking or if you're in an unfamiliar bed.