That’s a great question! We’re not entirely sure why we dream, but scientists have come up with some theories. One theory is called the ‘activation-synthesis’ hypothesis, which suggests that during dreams, various parts of our cortex are activated by input from very low-level areas of the brain, plus incoming sensory information from the environment around you (like a song that might be on your stereo, or noises outside your window). Dreams are basically your brain trying to make sense, or a story, out of all this information it’s getting.
Another theory is called the ‘continual activation hypothesis’. Some scientists think that the purpose of sleep is to help you transfer events from the day from short-term memory into long-term memory. This implies that, even though we’re asleep, our brain is still active and doing things. Some parts are helping out with this encoding process, which means that others might effectively be ‘disconnected’ from sensory input areas for a while. That means that other areas of the brain might be generating information – in other words, it’s wandering around without any feedback from sensory areas to tell say things like “hang on, that’s not possible!”. That might be one explanation as to why we can do fantastic things in some dreams.
There’s still lots of ongoing research into why we dream though, so new theories might come up!