There are a variety of theories about why we dream, such as to forget, to keep our brains working, or to rehearse and clean our synapses, but there is no singular reason
During some sleep stages, it sends information to the cortex but not receiving any in return, allowing memories to be transferred for long-term storage