When people sleep, brain cells also enter a state of relaxation and rest, but some brain cells are not completely rested, and weak stimulation will cause their activities, thus producing dreams.
For example, there is one thing that particularly excites me during the day and I still think about it before going to bed. When other nerve cells in the brain are at rest and these nerve cells are still excited, I will have a dream with similar content, which is called "thinking day and night".