Connect an external display, close lid and avoid sleep on MacBook

Apple provides a solution for this, actually.

These are the instructions, assuming you are using a Wired USB keyboard and mouse:

  1. Make sure your Mac notebook is plugged in to an outlet using the AC power adapter.
  2. Connect a USB keyboard and mouse to your Mac.
  3. With your Mac turned on and the display open, connect the Mac to the appropriate port on the external display or projector and turn the display or projector on. Use an appropriate Apple video adapter if necessary.
  4. After your Mac notebook's Desktop appears on the external display, close the computer's lid.

    When you close the lid:

    • In OS X Lion v10.7 and later, the external display changes to a blue screen, then shows your Desktop.
    • In OS X v10.6.8 and earlier, you can wake your Mac by clicking the mouse button or pressing a key on the external keyboard.

Effectively, you simply require AC power, and a mouse and keyboard connected, in order to make the display stay active.