Recent mention in New Scientist filled in some more detail on dreams.
Our broad understanding is that dreams are a way of processing the day's events, cataloguing and storing them away. And that dreaming happens only in the REM (Rapid Eye Movement) phase of sleep. This accounts for about a fifth to a quarter of sleep time, in about four bursts. I remember hearing of an experiment that found a stimulus of a ringing bell (that woke someone up) was incorporated into the final part of a dream that was quite lengthy. The report suggested this meant dreams were actually quite rapid.
In fact, dreams have been found in both REM and non-REM states - but they each seem to have different content and purpose.
A key idea reported by New Scientist is that REM dreams are a way of dealing with experiences: good or bad, the experiences are relived (and filed away) without the accompanying stress, thus dampening down the emotional impact of those experiences, helping us to achieve an equilibrium over time. REM dreams are more narrational, emotional, and aggressive. The suggestion is that the dreams with aggression help us cope with real aggression. Many such dreams involve unknown males in aggressive interaction with the dreamer. REM dreams also improve our memory and problem-solving ability - presumably through background storage, retrieval and calculation functions.
Non-REM dreams, by contrast, often involve friendly interactions, suggesting they foster co-operative behaviour.
New Scientist report, with references, here.