In sleep we leave behind the sensory stimulation of the outside world. A part of the brain called the thalamus, involved in the regulation of sleeping and waking, plays a crucial role in shutting out somatosensory stimuli and allowing the cortex to enter sleep.
One theory offered to explain hypnogogic hallucinations is that the thalamus deactivates before the cortex in human beings, so the still active cortex manufactures images, but this is just a hypothesis.
What is clear is that going to sleep involves making a psychobiological transition. Anxiety, guilt, excitement, a racing bedtime imagination, fear of dying, pain or illness can keep us from toppling into the arms of Morpheus. Depression often involves sleep disturbances, especially waking up early in the morning and not being able to get back to sleep. Weirdly enough, keeping a depressed patient awake for a couple of nights in the hospital can alleviate his symptoms temporarily. They return as soon as he begins to sleep normally again.