Minecraft wiki entry for Beginner%27s_guide, associated text: If a hill is nearby, you can dig a short tunnel into the hill and block off the entrance. This gives you enough time to place a crafting table and make a few fences, which you can use to replace the blocks at the entrance (one at a time so nothing gets in), so that you can see when dawn comes. Lacking fences, you can leave a one-block hole above head height at the entrance.
While digging straight down is usually unsafe, an emergency may warrant digging straight down three blocks from the surface and quickly blocking off the top. If you have already made some fences or trapdoors, these are ideal for the top, as, again, they let you see when dawn comes.
You can ride a boat out into an ocean to avoid the monsters on land, but there may also be monsters under the water (see Drowned). At the least, avoid any areas lit from underneath the water, and in a boat you can easily move away from any monsters that appear. Also, hunger does not deplete in a boat.
If desperate enough you can change the difficulty to "peaceful mode" temporarily to remove all monsters, and change it back in the morning.