How do I get out of the Dream or Nightmare dimension?

Minecraft 1.14.4 and up

After a set period of time, you will “wake up” at the bed where you started. This time is about 20 minutes. A good visual indicator is when the sun sets in the Dream, or the sun rises in the Nightmare. If you die inside either dimension, you will also re-spawn at your bed. Regardless of this, you may force yourself to wake up by using any bed inside the dimension.

Minecraft 1.13.2 and below

The only way to exist the dimension is to have a spare strange, luxurious, or wretched bed and use it in the dimensions, or the last resort way to leave is to die.