For 11.10 and newer:

You can select "Do Nothing" in the power settings. Open the dash (super key), search for "power" and then select the correct option in the dropdown. Note that closing the lid will actually turn off the screen though plus any external monitors:

If you move the mouse then any connected monitors will power back up with the laptop closed. This will then become the primary monitor.

systemd is now many things. Ubuntu does not use systemd's init ("pid 1"), but it does use components that were formerly independent but are now absorbed into the systemd source, such as udev. So you will see mentions of the systemd name on an Ubuntu system, but that does not mean that its init system has suddenly changed.
–
Robie BasakOct 17 '13 at 8:14

There is a bug with some laptops and monitors that makes Ubuntu not honor the "do nothing" setting when the laptop's lid is closed. I have not been able to find a complete solution, but maybe there is something to do with this link where they hint at this file : /etc/default/acpi-support and using:

I like to suspend my laptop sometimes, and other times I like to keep it going for long times like a server (do nothing when I close the lid). I use Debian, and here's my simple script to do either one on the fly without rebooting:

# run this in the as the same user (or root) that xwindow is using
# test for required parameter --> empty not allowed
if [ "$1" == "" ]; then
echo "Please provide true or false"
echo "True means keep running when lid is closed"
echo "False means suspend the computer when lid is closed"
exit;
fi
# "running true" means keep running even if the lid is closed
if [ "$1" = "true" ]; then
gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action nothing
gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action nothing
fi
# "running false" means suspend the computer!
if [ "$1" = "false" ]; then
gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action suspend
gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action suspend
fi
# if "echo" is supplied as a param, just show the current settings
if [ "$1" = "echo" ]; then
gsettings get org.gnome.settings-daemon.plugins.power lid-close-ac-action
gsettings get org.gnome.settings-daemon.plugins.power lid-close-battery-action
fi
# restart gnome to make changes effective
/etc/init.d/gdm3 restart