Why do we dream while sleeping?
Disclaimer: the shortest answer still remains we don't know...yet.

Talking from the evolutionary perspective, I have found as many as 8 theories about the purpose of dreaming, some of which even contradict each other(!). But I'll describe them one at a time.


One obvious theory is that we dream about something we dream of. So, dream is basically a way of wish-fulfillment; what we are dying to achieve, we get that in our dreams. Though it has been made quite scientific by Freud et al, it still doesn't look much scientific, at least from evolutionary perspective.
Another idea, made popular mainly by Hobson et al, is that dreams are purely because of accidental firing of neurons. In what they call activation-synthesis theory, they claim that during sleeping, neurons in areas of brain, including brain stem, limbic system, secondary visual cortex, etc. fire accidentally. When the brain tries to interpret those signals, the result comes out as dreams.
Another theory, proposed by Zhang et al, is that dreams are our brain's way of converting short-term moemory into long-term memory. This quite explains why some people reommend a power nap after learning something new, it helps the brain in storing the knowledge as long-term memory. Also, recent studies have concluded that it is better to remain awake for hours after a traumatic experience rather than sleeping right off, if you want to forget that experience faster.
Another theory, popularly called reverse learning theory, basically says that we dream to forget things. Dreaming is a way for our brain to prevent information overload so that memorizing new things becomes easier. Seems the opposite of Zhang et al and most likely answers why Sometimes we can't even recall what we dreamt of.
Yet another theory, seemingly more sound from evolutionary viewpoint, says that dreams are just like the playing-dead response of our body. When we dream, the neurotransmitters related to movement are shut down, resulting in what seems like a dead body. So, our body preferred to retain this mechanism because it could've helped in a way like the freeze response did.
A theory, proposed by Revonusuo et al, says that dreams are to make people ready for real-life situations. According to it, people who often dream about being chased by a tiger will learn how to overcome that situation and eventually will be able to survive such case. Seemingly answers your question Why do we sometimes get weird and unusual dreams?
Barrett et al, based on Revonusuo's theory, proposed that dreams are a way for solving problems for humans, partly because dreaming mind makes connections faster than awake one. Though this explains how Kekulé predicted benzene's structure, it doesn't look very much scientific.
A yet another one, by Coutts et al, says dreams are a way for "natural selection" of best possible method to solve a problem, including emotional situations, and is called Oneiric Darwinism. A much similar idea by Hartmann et al is called contemporary theory of dreaming.


I will not discuss the mechanism of dreaming, not only because it will make the answer too long or because others have delved into that point in their answers, but because it will answer the How? part rather than the Why? part, and also because that part would be better suited for cogsci.SE.