That is a very interesting question Danay, and despite many people wondering the same thing, we still are not sure! Scientists have come up with a number of possible explanations for why we dream. They can be summarized as follows:
1: Our brain remains active during the night while we sleep, and dreams are just a random byproduct of that activity.
2: While we are asleep, our experiences of the day are sorted such that some are stored as memories and others are erased. Dreams are a glimpse of this sorting process.
3: During sleep, our brain plays out fight or flight scenarios to help us practice for when dangerous situations happen in real life.
4: Dreaming allows our brain to process emotions in the "safe space of slumber".
Considering that most animals probably dream, the theory that makes the most sense to me is the first one (although maybe it is the most boring). Check out this video of an octopus caught “dreaming”. Octopus dreaming.
The octopus rapidly changes the structural color of its skin as if it were camouflaging itself in different environments.
Here is a recent article (April 2020) that summarizes the major theories for why we dream if you want to read more: