So where do they sleep?
Most modern longhaul aircraft have designated rest areas for the flight crew with bunk style beds where they can take proper sleep. These are usually located close to the flight deck itself, in the attic space above the passenger cabin, or in a lower compartment near the tail.
What about flights with just two pilots?
Again, this varies from airline to airline but on night flights with only two pilots, some airlines permit one pilot at a time to take rest while in their seat. This is usually limited to about 30 or 40 minutes so as not to go into a deep sleep which would prevent them from being immediately alert in case of an emergency situation.
What happens if both pilots fall asleep?
Well thankfully this very rarely, if ever, happens. Just in case it was to however, most longhaul aircraft have a system built in that will sound an alert chime if no buttons in the flight deck have been pressed within a prescribed time period.