While the default setting on a MacBook is to switch off the screen and go into sleep mode after a reasonable period of inactivity, it is possible to stop Mac from automatically going to sleep.
In general, the reason for preventing Mac from sleeping would be to allow the computer to perform tasks like Long file downloads, Data Backup to iCloud or External storage drive and running processes which should not be interrupted.
Stop Mac From Sleeping When Display is Off
The inactivity period, after which the Mac automatically goes into sleep mode is 2 minutes while on battery and 10 minutes when the Mac is plugged into a direct power supply source.
If this does not suit your requirements, macOS provides the option to increase the time or duration after which your MacBook, Mac Mini or iMac goes to sleep.
However, you need to be aware that preventing MacBook from sleeping while it is not plugged-in to a power supply source can drastically reduce its battery life.
1. Prevent Mac from Going into Sleep Mode
If required, you can prevent your MacBook from automatically going into sleep mode using the following steps.
1. Click on Apple Logo in the top-menu bar and select System Settings in the drop-down menu.

2. From System Settings screen, go to Energy Saver > Power Adapter and uncheck Prevent computer from sleeping automatically when the display is off option.

This ensures that your MacBook will never go to sleep, when its display is switched off and it is plugged-in or connected to a direct power source.
As mentioned above, selecting the “Never” or Longer duration for sleep mode can have an adverse impact on the Battery Life of your MacBook.
2. Increase Duration After Which Mac Goes to Sleep
Another way to deal with the issue of Mac going into sleep mode after a short period of time is to increase the duration after which the display is switched off.
1. Open System Settings > go to Energy Saver > Power Adapter and use Turn Display Off After slider to increase the duration.

2. Next switch to Battery Tab and use Turn Display Off After slider to increase the duration after which your Mac can go into sleep mode.

Note: Increasing the duration after which Mac goes to sleep while using Battery can have an adverse impact the battery life of your device.