This is known as information processing of dreams.
According to this theory, Our brain provide us with several unprocessed stimuli that we got during the day.
The information process that happen based on those stimuli is what we've perceived as dream during the night.