A MacBook doesn't need to be open to be useful. Connected to an external display, it can run while it's closed and act like a Mac Mini. By itself, a MacBook can run when closed with the help of a third-party app. You may want to do this so you can tote your MacBook from meeting to meeting at work without needing to reconnect to your network each time you reopen it. Or perhaps you want use it as a server, or just to play music to an external speaker with it tucked away.
1. Connected to external display
When connected to an external display, your MacBook will keep running if you also have it connected to a wall outlet and wired keyboard and mouse. You can use a wireless keyboard and mouse as long as you go to System Preferences > Bluetooth > Advanced and check the box for Allow Bluetooth devices to wake this computer. When you see your MacBook's desktop on the external monitor, you can close your MacBook and it will keep running.
- See this Apple Support page for more information.
2. Using InsomniaX to prevent sleep
MacOS does not have a setting that lets you override the default behavior of entering sleep mode as soon as a MacBook's lid is closed. Enter InsomniaX for Mac.
InsomniaX is a free app that places an icon in the menu bar that you can use to disable sleep mode when the lid is closed as well as when your MacBook sits idle. With Disable Lid Sleep checked from the menu bar icon, you can close your MacBook and keep it running.
I use it to play music and can happily report that the Bluetooth connection remains active along with my music source (be it Spotify, YouTube, iTunes or whatever) so I can pump out the jams to a Bluetooth speaker from my closed MacBook Pro.
Lastly, I would be remiss if I didn't end this post with a warning: Running your MacBook while it's closed could lead to it getting too hot. MacBooks vent air from their back edge, and the opening is smaller when the lid is shut. If you feel your MacBook is getting uncomfortably warm operating in this manner, you may want to disable the sleep override or use it for shorter amounts of time.