While it can be handy to have your Mac sleep, or the screen turn off, after a set amount of time, there are certain situations where you don't want it to fall asleep by itself - Like when you're watching a long video, or when you're downloading a large file and want to be able to see it's progress. It is incredibly annoying having to swipe your finger across your Mac's trackpad every thirty seconds when the screen dims, so this article has been written to show you how to stop your Mac from falling asleep when you don't want it to.
The first way to stop your Mac from going to sleep by itself is obviously to turn off auto display-sleep in the system preferences. To do this open the system preferences, click on the "Energy saver" panel and drag the "Display sleep" slider all the way across to "never" (on the right-hand side).
Your Mac will now only go to sleep if you manually sleep it (or shut the lid if it's a laptop). And don't worry about the warning below the Display Sleep slider in the system preferences saying "Never letting your display go to sleep may shorten its life". Although the LEDs in your Mac's display will wear out eventually if used constantly, their incredibly long life (13+ years if used for 10 hrs per day in mild conditions) will far exceed the life of the rest of the computer. By the time their life comes to a close, your Mac will either be obsolete and useless to you, or one of the other components (that has a shorter life) would have already given out.
While stopping your Mac's screen from turning off by disabling it in the system preferences is ideal for some, others may only want their Mac to not-go-to-sleep in certain situations, such as when they are watching a long video or keeping an eye on something. Having your Mac go to sleep when it hasn't been used for X amount of time is useful for various things, such as security and saving electricity. So: how does one let their Mac go to sleep by itself when they want it to, but not let it when they don't want it to? Answer: there's an app for that!
There is a free application available for Mac macOS (and OSX) called Caffeine. Caffeine displays a coffee-cup icon in the top menu-bar that you can click on to stop your Mac from going to sleep. So, for example, if you start watching a long documentary on the Internet you could click on the coffee-cup icon and your Mac will not go to sleep by itself until you've clicked on it again to re-enable auto-sleeping.