They do this using the accelerometer and gyroscope sensors built into the devices. (lifewire.com)
The phone though misses on Gyroscope sensor so VR headset can not be used, also it does not support slow - motion video recording. (suggestphone.com)
The iPad 2 also adds the same three - axis gyroscope sensor shared by the iPhone 4 and iPod Touch, giving the device a more detailed sense of its orientation in space, similar to the feeling of holding a Nintendo Wii remote. (cnet.com)