By default, macOS is set for maximum power efficiency. It tries to preserve energy as much as possible so that your battery life will last longer and your electricity bills will be lower. As a result, when you stop using your Mac for a certain amount of time, it automatically goes to sleep.