There are many ways to answer this question. Most answers I've seen, from various fields from neuroscience to psychoanalysis to common, address some part of the functions of dreams and contain a good deal of truth. What we lack at the moment is a coherent theory as to why it is necessary, especially in the form it exists. What would we not be able to do if we didn't dream at all? What would we not be able to do if we dreamed differently?
As explained in a previous post - Structure and Interpretation of Human Minds - the tasks that the mind has to perform for us to even deal with the basic everyday life are rather overwhelming in scope. At the immediate level, it has to process sensory information and respond in real time. In order to respond properly it needs an elaborate model of reality.
The Structure of Reality
What we commonly call "reality" in conversations is what I refer to as a model of reality - or some common subset (or sometimes a superset) of such models within some population. For instance, I may say that Peter the Great was a real person, or in a more convoluted way, that the historical existence of Peter the Great is part of reality. What we're saying is that we have accepted as part of our model of reality that he existed in the form we have generally modeled him through our common imagination. The earth rotating around the sun, various physical laws, etc, are all aspects of our model of reality, aspects that are accepted by a large percentage of the human population.
But not all modeling happens in the form of strict propositional truths. Ultimately, what's more important than modeling the world propositionally (by encoding reality as a series of factual propositions) is mapping situations to reactions. Propositional truths, at best, can be seen as a tool that helps accomplish this mapping more efficiently. While simpler cognitive systems (many animals, many computer programs, etc) can be wired to react to specific stimuli, the cognitive problems we deal with are far too complicated without using propositional truths. It is because our modeling has reached this level of sophistication that we have a concept of reality.
Then, our model of reality can be seen as some sort of computational network populated by a large number of heuristics, some of which encode propositional truths and others code for reactions to situations. One important point from my earlier post Structure and Interpretation of Human Minds was that the mind does not clearly separate propositional truths from behavioral heuristics and all sorts of random heuristics get encoded as truths as long as they support seemingly advantageous behavior. These are, in some sense, indistinguishable from other propositional truths. In fact, all propositional truths are encoded as such ultimately for this reason - it's just that we do enjoy being right and/or being confirmed by others. Thus in domains where other emotional forces are weak, our model of reality tends towards truths that are verifiable by others as such.
One may ask, at this point, how does this model of reality in terms of these heuristics encoding truths and situational behavior - let's call those rules - get used? Let's take an example of someone driving. The driver has to be constantly aware of the causal consequences of his own actions. He has to know what the gas pedal does and what the steering wheel does. He also needs to predict how other cars will move, based on his experience, based on his knowledge of the traffic signals, etc. These rules combine to give him a sense what is likely to happen - they limit the range of possibilities his mind must consider in real time. Then the rules that govern his behavior kick in to guide his actions towards more positive possibilities. Note that deciding to pay attention or spend time thinking about something itself is immediate reaction that needs to be learned. He cannot afford to figure out, when he's about to run over a pedestrian, how much time he has to consider what to do. He must react, and he must have already known that's what he needs to do. On the whole, those rules code for the range of possible future situations at any given moment (in other words, cause and effect or how events progress over time which is why causality is such a significant part of our model of reality, despite poor logical justifications for it) and proper reactions (in some cases it could just be deliberation - you may not have already the answer to some math problem seen during a test, but at least you know what do) to them. This is, effectively, reality.
Reality and Dreams
What does any of this have to do with dreams? My contention is that dreams make all of this possible. What's even more overwhelming than the number of rules that constrain possibilities is the sheer range of possibilities before applying the rules. The rules we use to generate possible realities in our mind must both be sufficiently narrow to exclude outrageous ones (which allows for faster reaction and a lighter cognitive load) yet not neglect even unlikely possibilities that have significant damage potential. Having a wrong set of rules could be disastrous - the driver may not see that the truck coming his way is on a collision course, or that a toddler is not looking his way and won't get out of the way. Yet, from a computational standpoint, sometimes the only way one can tell if one rule is better than another is to try out both on some level. This is where dreams come in - dreams provide a safe testing ground for new rules. Thus it is during dreams where we figure out what reality is. We make up all kinds of alternate rules for reality (how things are) and for behavior (how we should respond), and figure out which ones make sense and which ones don't. To some extent, daydreaming (or thinking to a lesser extent) serves a similar purpose, except that some rules are difficult to train under this scenario, since the mind must perform a lot of other cognitive tasks when awake - this increases the possibility of inappropriate or even dangerous reactions (or non-reactions) to external stimuli. If your imagination is too vivid, you may not be able to respond properly to physical stimuli; if your imagination is not vivid enough, you're not able to train certain rules or consider larger changes in the rule set. Those with schizophrenia, for instance, appear to daydream in an uncontrollable manner with nearly the same level of vividness as others dream at night. Part of the rigidity of human sleeping/dreaming patterns, some of which may seem computationally inefficient probably has to do with the extreme difficulty of determining the meta-rule that determines when it's okay to dream - it's difficult to simulate the effects of dreaming itself within a dream. One speculative hypothesis I have is that at least some cases of mental illness may be caused by irreversible meta-training of the process of dreaming itself - it may allow the entire computational stage for determining reality to be hijacked by a narrow emotional need.
Other thoughts
I've recently heard the Jessica Alba refutation to a related general hypothesis that dreams serve an active cognitive purpose. It goes like, if dreams help you prepare for reality, why do I have a threesome involving Jessica Alba and Snow White in my dreams, like I need to prepare for that possibility? This refutation directly supports my hypothesis. You know it's ridiculous precisely because you have dreams along that line. Our rules for figuring out what's absurd or not must be developed as well. For nearly any absurd dream, you can probably find a mentally ill person who believes in a similarly absurd reality. If you don't take for granted that you know the difference between real and absurd, learning what's absurd requires simulating absurd scenarios, at least in some cases.
Beyond absurdity, the other common theme in dreams is threats. This is also expected by the hypothesis. Reality needs to be most precisely modeled (and our responses most intensely rehearsed) in potential situations where significant action is required and incorrect action is costly. Thus the mind is biased to remember and generate rules based on emotionally intense scenarios.
What I have not attempted to explain is why we remember some dreams but not others (most) - most likely this has no clear computational purpose; remembering dreams is likely one of those slightly incomplete adaptations that do not have significant adverse effects and thus remained. Again, because of the difficulty of meta-training of the process of dreaming itself, a lot of the specific mechanisms of dreaming, other than the requirement that the mind not have to perform any significant cognitive task at the same time, are probably not particularly adaptive, merely not dramatically maladaptive.