Our ability to maintain sleep or wakefulness is because of "mutual inhibition" between the wake-promoting neurons and the sleep-promoting neurons. The areas in brain which regulate wakefulness by activating the cortex also inhibit neurons present in ventrolateral preoptic nucleus (a cluster of neurons situated in the anterior hypothalamus). And when VLPO neurons are activated, they induce sleep, and this also inhibit activity in the arousal centers like neurons in tuberomammillary nucleus (TMN).
Transitions between these states of wakefulness and sleep is very rapid, usually in seconds. Various researchers have reported this neurological mechanism for "flip-flop switch" in form of electrical circuit.
The mechanism for mutual inhibition is regulated by sleep drive or the circadian signal. When one of this body signal is sufficiently strong, it transits to the opposite state from wakefulness to sleep or vice versa. This mechanism is analogous to mechanism involved in transition between rapid eye movement (REM) sleep and non-rapid eye movement (NREM) sleep.
Humans require some time to relax and then fall asleep, and deep sleep needs 20 or more minutes after sleep onset. Sleep onset and associated loss of consciousness is related to one another and occurs instantly.
Many intrinsic and extrinsic factors regulate sleep cycle. Adenosine is one of the chemical which accumulates during wakefulness and later induces sleep by inhibiting wake-promoting neurons. And caffeine inhibits adenosine activity and helps in wakefulness.
The time needed for transitions between sleep and wakefulness is maintained by biological clock located in the suprachiasmatic nucleus. This is small structure consisting of about 50,000 brain cell. And it receives light signals from eye via optic nerve. Light controls the day-night cycle and which in turn regulates various biological functions including temperature, hormone release, sleep and wakefulness. The SCN stimulates wakefulness by producing a alert signal which inhibits sleep. The SCN also helps in inducing sleep by turning off alert signal.