Open the terminal and run the below command
pmset -g | grep hibernatemode
Note down the number in safe place, you'll need this later if you want to disable it
Now, run the below commands one by one.
sudo pmset -a sleep 0
sudo pmset -a hibernatemode 0
sudo pmset -a disablesleep 1
If you close the lid of your macbook now, it won't go to sleep mode
Incase you want to disable this behaviour, run the commands one by one
sudo pmset -a sleep 1
sudo pmset -a hibernatemode <original hibernatemode value from the above step or usually 3 on a MacBook>
sudo pmset -a disablesleep 0