For best results calibrate the accelerometer in your device by tracing out a figure of 8 in the air several times vertically and horizontally. The heading can also be affected by nearby magnetic fields.
For more information visit brujula.net.
Unfortunately this browser doesn't support orientation so will not show your correct heading.