Q:

How to shut down RPi when running headless

If I am running a Pi headless, is there a command I can use to safely shut down, or should I simply remove the power cord?

A:

You can safely shutdown the pi using 
shutdown -h now

The -h just halts all processes

A:

Do not simply unplug the cord, as this could occasionally (perhaps, often) lead to filesystem corruption.
As Impluss says, use shutdown.  I recently ran across a tip about configuring udev to trigger shutdown or reboot when a specific usb device is unplugged.  This is useful if the system has become unresponsive or has lost a network connection and you can't or won't bother with plugging hid (human interface device) stuff like a keyboard into it.
There is a good, perhaps mildly outdated but well written, introduction to udev rules |here|.  The basic idea is you get some information about the device via lsusb, for example:
Bus 002 Device 003: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN

The third field labelled ID is the vendor and model id separated by a colon.  Presuming you do not have multiple identical devices plugged in, this combination should be unique.
You can get more detailed relevant informationan via udevadm monitor --udev --property, which will report to standard out until you kill it, eg. when I unplug the teenie weenie wifi dongle from above it spits forth:
UDEV  [2834.504860] remove   /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.6 (usb)
ACTION=remove
[...]
ID_BUS=usb
ID_MODEL=802.11n_WLAN_Adapter
ID_MODEL_ENC=802.11n\x20WLAN\x20Adapter
ID_MODEL_ID=8176
[...]
ID_VENDOR=Realtek
ID_VENDOR_ENC=Realtek
ID_VENDOR_ID=0bda

Notice the ID_MODEL and ID_VENDOR fields.  These are what you want to use in your udev rule.  There are some outdated or incorrect sources online that suggest using ATTR fields, but these are ENV fields with regard to a "remove" event.
Create a file in /etc/udev/rules.d.  This is the same regardless of distribution.  The file must end in .rules and all files in this directory are processed lexicographically.  I believe rules declared earlier take precedence, so using 00-my_pi.rules will keep it at the front of the line (numbers sort before letters).  In it add a line like:
ACTION=="remove", ENV{ID_VENDOR_ID}=="0bda", ENV{ID_MODEL_ID}=="8176", RUN+="/sbin/shutdown -h now"

Beware == and not =. If you use the later, the criteria is meaningless.  In which case you could end up with a udev rule that matches any event!
Make sure this is loaded with udevadm control --reload-rules.  Now when you yank the wifi dongle out, the pi should cleanly shutdown...give it a minute to do that and you can then unplug the power (try this with a screen attached the first time).  You could also use this to reboot -- see man shutdown, and, actually, the man page for all the commands mentioned here ;)

A:

You may issue the following command to shutdown:
sudo init 0

And to reboot:
sudo init 6

