I was curious about this a few years ago and looked it up. It's called hypnagogia and people have been known to try to induce it deliberately for creative purposes.
Gave it a Google. Explains my other ranting post in this thread quite well. I apparently spend quite a lot of time in this state. Get lots of really good ideas (almost as many as when doodling on the condensation of a foggy shower window).