Humans stretch when they wake up because the human body protects itself while sleeping by inhibiting nerve impulses to muscles, prohibiting any from overextending during sleep. Stretching moves the muscles beyond their normal range of movement, allowing the body's feedback mechanisms to return to the waking state of motion.
In addition to the limited nerve impulses that prevent the muscles from overextension during sleep, the need to stretch upon waking is also prompted by the body's fluids pooling towards the back of the body. Stretching allows the fluids to return to the muscles. After stretching, the muscles become responsive and ready for movement.