Once brain cells have moved all of the day’s new memories from short-term to long-term storage, the NREM cycle is complete. Now, the brain moves into the REM state of sleep. It is during REM sleep, that our brain forms new ideas based on what was just learned that day.
REM sleep is responsible for making new connections between the newly stored memories in the neocortex with all other memories stored there from birth until the present moment. The neocortex is what governs our higher functions, from spatial reasoning and sensory perception to conscious thought and language.
Therefore, sleep is what helps us catalogue every experience and piece of information we have ever known. Night time is when our brain cells file away these new memories into their proper place so when our minds are awake, we have an easier time of connecting thoughts and creating new ideas.