How does the digital compass "figure of eight" waving for calibration work? What is the logic behind the "wave your device in a figure of eight to calibrate the compass"? How does this work?
The reason for moving your device in a figure eight is to help it calibrate itself against magnetic interference. By moving it slightly its able to detect the differences and return a more accurate reading. 

You can read the Apple knowledge base article here