I answered the following question on Quora:
Last night I slept for 8.5 hours and had a dream that lasted for a month. It was full of incredible landscapes, animals, and interesting interactions with people. I woke to my alarm at 9:30, silenced it, then went back to my dream for a week before waking up at 10:30. What was happening in my brain?
Looks like no one has mentioned hippocampal replay yet!
I’m not a big fan of the movie Inception, but there is some tentative neuroscientific evidence that the time distortions experienced during dreaming have measurable neural correlates. (Experiential time distortions are of course purely subjective, so no one can tell you that you didn’t experience them. All experiences are real experiences.)
There are neurons in the hippocampus called place cells that tend to fire when an animal is in a particular location. (Incidentally, the discoverers of place cells and grid cells won the 2014 Nobel Prize in Physiology or Medicine.)
Let’s say a rat is navigating through a maze. When it reaches point A, a particular cell (or group of cells) fires. When it reaches point B, another cell fires. So there is a sequence of neuronal firing patterns that corresponds to the sequence of locations that the animal has experienced. In the picture above, each color represents the firing of one place cell. So each place cell covers a region of the maze/track.
So what does any of this have to do with dreaming? Well, when the animal is in REM (dreaming) sleep, or is quietly resting, the place cells that were recently active become reactivated. These reactivations are typically much faster than actual experience. They can also run backwards relative to prior waking experience, and can even be jumbled.
Of course, your experiences in dreams are more than a sequence of places. To extend the insights from rodent place cells into the study of actual human dreaming, we have to make a few speculative leaps. Perhaps in humans, there are ‘experience cells’ or ‘episode cells’ that encode broad categories of perception and cognition. Many neuroscientists refer to the set of cells that participate in such categorization as a cognitive map. Sleep seems to involve a free-form exploration of the cognitive map.
Hippocampal replay is widely seen as crucial for consolidating memories, and for learning. If you’ve been doing something during the day, when you sleep or rest, unconscious neural processes help you extract useful information, so that the next day your performance can improve.
Most of the data on hippocampal processing come from animals. It’s worth remembering that we can’t know if animals have the kinds of dreams that humans do. Nevertheless, the picture of sleep emerging from various lines of inquiry suggests that dreaming may be a subjective ‘side-effect’ of various sleep-related neural processes such as hippocampal replay. (Nowadays most neuroscientists would refrain from claiming that the dreaming experience per se is the purpose of dreaming.)
Many neuroscientists think that one of the purposes of sleep is to replay the past, and thereby discover new possibilities for the future. Perhaps speeding up the process is an efficient way to cycle through multiple ‘angles’ on the past, or on the wider space of possibilities. No one has any idea why this process should have subjective experiential correlates — and we are even more in the dark about why these experiences tend to be so vivid and bizarre.