Minecraft wiki entry for Beginner%27s_guide, associated text: It lets you skip past the night (or a thunderstorm, which can also be a fairly dangerous situation for a new player). No time passes in the world: crops do not grow, furnaces do not smelt, etc., but the game time advances to dawn, upon which monsters stop spawning on the surface and any skeletons or zombies caught in the sudden sunlight burst into flame.
If you are in a multiplayer game, all other players must also use a bed before night is skipped, though various game mods can alter that rule.
Using a bed sets your spawn point, that is the location you respawn in if you die. Without a set spawn point, after dying you reappear somewhere, without your possessions, within 20 blocks of the world spawn point (usually outdoors), and if it was night time, it's still night when you respawn. This is not a pleasant situation! In current versions (1.17 or newer) of Java Edition, using the bed during daytime sets your spawn point even if you can't sleep yet.
If you have not slept (or died) within the last three days or so, Phantoms can spawn at night and even during thunderstorms. If you go outside, these swoop down from the sky to attack. Sleeping in a bed prevents this not-so-minor inconvenience. Phantoms burn like other undead monsters after the sun rises.