When you're sleeping your body goes into atonia during REM. This is when your muscles lose strength. Often times when you're sleeping in REM (Dreams!) your body would make movements and act out your dream if it did not go into a natural state of atonia. So! When you wake up in the middle of a dream and you feel paralyzed it's generally because of this. (Otherwise described as sleep paralysis)
When you're about to wake up your body produces orexin, which is a hormone that promotes wakefulness.
http://www.end-your-sleep-deprivatio...waking-up.html
^ That is a more detailed response about what I described. n.n (It's easier to read, too)