People are usually surprised to know that, despite decades of research, science has no clear answer to the above question. The real function and evolutionary origin of sleep remain a mystery.
Here I would like to offer two perspectives. The scientific and the spiritual ones are not mutually exclusive but might well complement each other (even though science believes to be the only truth and obviously excludes everything else).
From the scientific perspective, I suggest reading the article of Aeon. It summarizes quite well the state of the art of neuroscientific research on the subject.
However, it turns out that scientist can’t wrap their head around that. Initially, they try to sell us some partial truths as scientifically established facts that seemingly explain everything. But if you read the article through you discover that their theory is yet again another speculation, conjecture, not much more than a hypothesis that does not go much beyond curve fitting. Nevertheless, it tells us something about the “Nature-side” of that strange habit we call “sleep”.
What follows will describe the “Spirit-side” of sleep. It is an extract from my book “Spirit calls Nature”. I hope that at the end you will be able to make a synthesis of knowledge of the two points of view. It is time to integrate spirituality with science, not by explaining the former with the latter, rather by taking the two perspectives at once and going beyond both.
While for the ordinary human consciousness in the waking state, our outer being is all that we believe to be and is indicated as ‘me’ and ‘I’, during the sleeping state, we retire from the outer into the inner or inmost being or into the subconscious or supraconscious planes (the higher hemisphere we will describe later).
As it also appears in the writings of Sri Aurobindo, dreams are (in most cases) a distorted symbolic transcription of the experiences of our consciousness moving throughout the different planes of being. In this respect, he aligns with traditional Indian psychology. While the number of possible states of consciousness is virtually infinite, each of these states can be related to four main categories.
- The common waking state (‘jagrat’) where the sense-mind and our physical mentality are most active and almost completely identified with our bodily existence.
- The dream state (‘svapna’) where the psychic being enters into a series of life-planes and mind-planes, detaching from its corporal identification that was predominant during the waking state, but still with a physical mind active and which translates all the experiences on these planes in the form of an incoherent jumble, wondering phantasies, disordered associations from brain memories, etc. We most easily recall these experiences on vital and mental planes as vague reflections in form of confused dreams because they are filtered by the same physical mind through which we interpret the physical plane.
- The (deep and dreamless) sleep-state (‘susupti’) where the soul is liberated from the outward-going senses and enters the supramental plane, also more commonly called the ‘casual body’. Here the self becomes what Aurobindo describes as the ‘Master of Wisdom and Knowledge’ (‘Prajna’).
- Finally, the supreme or absolute self of being, the ‘fourth’ state of consciousness (‘turiya’). It is a state of pure and absolute self-existence where the soul rests only for a relatively short period of (earthly) time before climbing back from this divine state to the ordinary waking state.
The gulf between our waking state and these other progressively deeper states of consciousness arises due to an untrained psychic being (the soul) that experiences this state of ‘trance’ as a blank to the waking mind. The aim of classical yoga is precisely to bring these deeper dimensions to the surface and even rest in the fourth state during the waking state.
Thus, dreams are experienced on the inner planes filtered through our minds. If the consciousness dwells in the inner being or higher planes, the mind translates it into vivid and refreshing symbolic dreams, while an experience on the subconscious plane is reported as a series of incoherent and heavy dreams and eventually nightmares. The short time interval during which we really find ourselves in a state of sleep without dreams is that of ‘psychic rest’—that is, when the psychic being retires into itself and momentarily detaches from all the outer and inner parts—and is the most refreshing part of sleep. Therefore, generally, dreams are the mind’s interpretation of experiences on the subconscious, vital, mental, psychic, or higher spiritual planes. If we find our waking-state daily experiences reflected in a dream, it means that they have been registered in the subconscious, which tosses them up once again when the waking mind is quiescent and does not filter them out.
In this view, metaphysically speaking, sleep is a necessary retreat of the soul from the burden of the material plane. Not only is it a physiological necessity, but it also has a psychological function ensuring that the soul goes back to itself from time to time. Sleep prevents our inmost essence from losing contact with its source and with other subtle non-physical realms. In fact, as is well known, sleep deprivation leads to severe psycho-physiological disorders and, in the extreme case, to physical death.
Another spiritual function of sleep is that of ‘recharging’ the life-sheet, or ‘vital body’ with its vital energy. When we don’t sleep enough, we increasingly feel a sense of lack of vitality, energy deprivation, and inertia that no substance, food, or physical means can regenerate other than the sleep cycles, which restore a balance in the vital sheet with a vital force. This is, obviously, something modern science resolutely rejects. Nonetheless, intuitively we feel and know this from our daily waking-sleep experience but misinterpret that feeling and sense of weakness as purely physical because that’s what we are told to believe.
Modern science is, in some sense, right in describing dreams as having the functional role of processing our emotional waking-life experiences to avoid an informational and experiential overload that we could otherwise barely handle. While scientists agree that sleep has the purpose of repairing and reorganizing neural pathways, consolidating memories acquired during the waking state, filtering out redundant information, restoring the body and mind, and serving other physiological and psychological functions, there is nevertheless a shared consensus that this can’t be the whole story and that the real function and purpose of sleep remains elusive (for a review, see , ).
In fact, we don’t know why all the organic functions of sleep mentioned above couldn’t be performed in a waking state as well? But most puzzling is its evolutionary function. From a naturalistic evolutionary perspective, sleep looks like a bit of an outsider. According to Neo-Darwinism, everything—including sleep—must have evolved out of natural selection and random mutations to allow for the best survival and reproduction chances. But sleep is a risky habit in a prey-predator environment, especially if you are at the bottom of the food chain. Yet, there is no living organism, from cyanobacteria all the way to humans, that isn’t subjected to a circadian clock that, in brains, expresses itself as what we call ‘sleep’. There is no evidence to suggest that natural selection favored those able to stay awake longer.
Moreover, the naturalistic theory that identifies sleep as a brain cycle and a cerebral necessity is as questionable as the physicalists’ mind-brain identification itself. In fact, waking-sleep activity is not limited to organisms with a brain; those without a brain also show waking-sleep cycles. A sleep-like state has been observed in the cnidarian Hydra vulgaris, a small freshwater polyp with only a primitive nervous organization.  It is now known that sleep is also present in animals, such as the jellyfish Cassiopea, which possess neurons organized into a non-centralized nerve system but that has no brain. Their pulsing behavior, alternated by periods of quiescence at night, is consistent with waking-sleep cycles. When deprived of these quiescence periods, their activity and responsiveness decrease, indicative of a sleep-like state and supporting the hypothesis that sleep arose prior to the emergence of a centralized nervous system. 
Of course, by speculating about any sort of evolutionary advantage and adaptive benefit, one can always imagine something that could explain away sleep’s function in the orthodox evolutionary Darwinian paradigm. For example, we might think of sleep as something that keeps us fit by ‘charging our physiological batteries’ so that we can escape the predator during the day or avoid dangers that hide in the darkness. But what is it that ‘recharges’ during sleep? Why can’t we recharge in a waking state? And the day/light cycles alone can’t be the only reason for the wake/sleep cycles. There are also nocturnal animals. Then there are animals living on the poles, in the deep sea, in caves, or under the earth, where no such cycling exists and all, without exceptions, are nevertheless subjected to some circadian cycle. Fill this in with dozens of other hypotheses and conjectures. Imagining so-called ‘evolutionary advantages’ is easy and is like finding the culprit to blame for everything. You will always find something to cling to, but proving it to be the real cause is another matter.
The fact is that the evolutionary function of sleep remains unclear to science. When confronted with the hard fact that non-neuronal forms of life also sleep, at the time of this writing, some opined that the most plausible hypothesis is that sleep serves metabolic functions.  That might well be the case, but we predict that there will always remain an explanatory gap, a deep mystery that refuses to disappear. The true nature of the sleeping consciousness and its dreamlike character will be precluded forever to science if it doesn’t accept and open itself to a first-person inquiry and research into other planes of being. Perhaps, the first step in this direction could be that of thinking the other way around: Sleep isn’t an adaptation; rather, it is an evolution that adapted to sleep because it is a metaphysical necessity of the inner being to temporarily disengage from the outer gross plane and reconnect with subtle domains. But, we believe, as long as science continues to ban the soul and vital forces as anathema, positing it a priori as inexistent, not because of a scientific rationale but because of an axiomatic and almost unquestioned assumption, it will hardly be able to go beyond a superficial understanding of the function of sleep and will continue to circle a mystery it can’t make much sense of.