The biological function of rapid eye movement (or REM) sleep—one of the great mysteries of neuroscience—remains unknown. Here, I demonstrate that the biological function of REM sleep is to heighten brain alertness, significantly reducing the high vulnerability of deep sleep. Every organism with a nervous system must undergo deep sleep: a necessity that comes with substantial vulnerability. Deep sleep, by lowering alertness, compromises the organism's safety, putting its survival at risk. Therefore, REM sleep is a necessary adaptation for any organism that must sleep. My primary goal here is to present a comprehensive conceptual framework—supported by extensive empirical evidence—to connect numerous disparate empirical findings under a unified theory of the biological function of REM sleep. Additionally, I also provide a historical narrative to explain its origin and subsequent evolution. The theory I developed to explain the function of REM sleep is not only widely corroborated but has also resisted numerous attempts at refutation. This allows me to claim that I solved this great enigma of neuroscience.