Sleep
When a creature that needs to sleep ends a day without, it must succeed on a DC 10 Constitution saving throw or suffer one level of exhaustion. Each consecutive day that passes without the benefit of sleep increases the DC by 5. To sleep, a creature subjects itself to the unconscious condition.
4 hours of unbroken sleep is enough to be beneficial. If you accumulate 8 hours of beneficial sleep over a day – you recover one Hit Die and if you slept comfortably, you can recover a level of exhaustion (you can't recover more than one hit die and level of exhaustion per day this way).
Broken Sleep
Your sleep is broken if you take damage, or another creature uses an action to shake you. Sudden loud noise – such as yelling, thunder, or a ringing bell – will wake most creatures, as can whispers within 10 feet of those with passive Wisdom (Perception) of 20 or higher, and normal speech for any with passive Wisdom (Perception) of 15 or higher where the environment is otherwise silent (no wind, birdsong, crickets, street sounds or the like.)
Making Yourself Comfortable
Medium or heavy armor is not comfortable to sleep in. Circumstances, such as exposure to bad weather, thirst, or starvation, can also stop a creature from being comfortable.
Attuning Magic Items
At the start of a period of sleep, you can choose a magic item to attune, if you have one. Provided the item is never more than 5 feet from you, once you accumulate 8 hours of beneficial sleep over a day it will be attuned.
Alternatives to Sleep
Some creatures have features that replace or modify sleep. For example, creatures that trance can subject themselves to the stunned condition, to count about 4 hours of trance as equivalent to 8 hours of sleep; and warlocks benefiting from Aspect of the Moon can perform other lowkey activity instead of subjecting themselves to the stunned or unconscious condition.