Throughout the night, we go through several sleep cycles (usually 4–6). Every sleep cycle usually consists of 90 minutes and consists of four stages.
At every stage, something else happens in our brain. I am enclosing a short and fascinating article that explains interestingly what happens at each stage of sleep.
Read more in the article: https://betternightsleep.net/2021/05/09/stages-of-sleep-cycle-and-sleepwalking/