You can do this thanks to a reflex called "Bell's phenomenon." Basically, it means your eyes move up and out when you try to close your eyelids. You can see this if you hold open your eyelids then forcefully try to close them. You can't see it yourself, but if you do it on someone else, you should see their eyes move out.
Why is this useful? Basically, it's a defense mechanism. If something is coming at your eyes and you clench your eyelids shut, you move the center of your eyes out of the way so that they won't get damaged.
This was first noticed in patients who had Bell's palsy, where half of their face was paralyzed. When they try to close their eyes, the eye on the paralyzed side of their face moves up and out, like int he video below. I've seen a few patients with Bell's palsy at the clinic where I work. They often complain of their eyes drying out because one of their eyes cannot blink. Fortunately, Bell's palsy is usually not permanent.
I remember doing this to David while he was sleeping. We got the Gameboy Camera and held his eyes open and took a picture, and I guess he was trying to close his eyes while sleeping, and his eyes rolled out like that. It was really funny/creepy.