Gyroscope
A traditional gyroscope consists of one or more spinning wheels mounted in such a way that they can rotate freely in any direction relative to what they are mounted to. The physics principle known as conservation of angular momentum dictates that the wheel(s) will maintain its orientation regardless of the movements of the mounting. By measuring the angle of the wheel(s) compared to its mounting, one can precisely measure changes in angle of the object the gyroscope it attached to.
In a phone, the gyroscope is an extremely tiny device that uses MEMS technology, which is like a microchip with moving parts.
Because a spinning wheel would be impractical at that size, a MEMS gyroscope uses multiple tiny weights on springs, that vibrate back and forth, each along a specific axis. Due to angular momentum, the weights tend to continue moving in the same direction, even as that device around them rotates. This changes the distance between the weights and electrodes around them, which can be sensed using small electric fields.
A gyroscope is similar to an accelerometer, but is much better at measuring angular rotation (spinning and twisting), while an accelerometer is better at measuring linear movement and orientation to the ground.
See: Accelerometer