"Whether awake or asleep, the brain constructs a model of reality—consciousness from the best available sources of information. During waking, those sources are external sensory input in combination with internal contextual and motivational information. During sleep, little external information is available, so consciousness is constructed from internal sources. These include expectations derived from past experience, and motivations-wishes, such as Freud observed, but also fears. The resulting experiences are what we call dreams. In these terms, dreaming is perception free from external sensory constraint, while perception is dreaming constrained by sensory input—hallucinations that happen to be true."
See also: "Wake Up and Dream," Radiolab, January 23, 2012