Many think its a way of replaying what your brain observed during the day and making sense of it all. We really don’t understand dreams or even consciousness. There have been MRI stiudies where scinetists monitor how much oxygen each part of the brian uses while dreaming, but it still doesnt explain the frankly nutty things that happen in your head every night
There are many theories behind dreaming like dreams are a way of processing and filing away all the sights, smells, sounds, tastes and events of the day. But no definite answers for your question as scientists are still trying to understand the science behind dreams. Interestingly, most of us spend 1½ to 2 hours dreaming every night. Scientist has proved this that the three things—dreaming, one special kind of electrical activity, and rapid eye movement (REM)—all go together. During these periods of “REM sleep,” you can tell that a person is dreaming without ever waking the person up.