No one knows!
There are many hypotheses, but no widely accepted theory of dreaming.
Many mammals dream—rats, cats, dogs, pigs, etc.—so humans, as intelligent as we are, aren’t unique in the capacity to dream.
It’s been proposed that lucid dreaming is a sign of mental evolution, but that idea is dampened somewhat by the evidence that humans have been dreaming (or at least seeking trance states) for thousands of years, and by the revelation that different cultures have different levels of lucid dreaming. Among some indigenous cultures, lucid dreaming is far more common than it is in the so-called First World.
Here’s an expert summarizing the knowledge of why we sleep:
ted.com/talks/russell_foster … sleep.html
The need of sleep itself is still a mystery; dreaming is an even bigger one.
A prevailing theory, though, is that dreaming is adaptive—that is, dreams allow animals an opportunity to practice, in a safe virtual environment, for the events of daily life.
A still more popular theory is that dreams foster the integration of memory.
Any attempt, however, to definitely pin down why we dream forces us to fit all our dreaming experience into a box. How can dreaming be practice for future events, or the integration of long-term memory, if a dream is (1) about a past experience—which we remember quite well, or is, perhaps, (2) precognitive?
Dreams come in many shapes. They can be almost ordinary: about us sitting at our desk in our office, with some strange problem; they can be fantastical, like a book or a movie, in which we’re a superhero who can bend time; and, they can be completely incomprehensibly abstract: about you, without a body, flying through a void with geometric colors blossoming around you. And, as I mentioned above, they can apparently be prescient, showing us future events. (A subject that divides many people.)
Aside from the many theories, and the many modes of dreams, however, is a growing body of psychological research that says dreams do indeed offer coherent communication within the psyche. For years—and this is still the general prejudice—people believed that dreams were essentially random. But that’s an outdated idea, disproved by a number of researchers with different aims.
What’s certain is that dreams provide real insight about your body/mind system. This can be as simple as showing you how you actually truly feel about a person, apart from your waking ego’s rationalizations. Or they can simply show us, when we pay attention, what we want or what we’re afraid of. The information isn’t always surprising, or completely unknown to the waking self, but—in my own experience—it’s usually complementary to what I think and feel consciously.
Why we dream is a difficult question to answer. But what dreams offer isn’t difficult to answer at all.