This whole system is based on an amazing technology called MEMS. It stands for Micro Electro Mechanical Systems. What it means is that inside a chip that is usually smaller than 1mm2 the manufacturers are able to squash not only the logic ICs, but also microscopic moving parts. Otherwise they wouldn’t be able to detect physical properties like acceleration, wouldn’t they?
Inertial sensors based on this technology are inexpensive, and they deliver amazing results, but they have the drawback of having some inherent noise and offset on the delivered values. In addition, magnetometers are easily disturbed by the presence of metallic masses on the surroundings generating large distortions of the perceived magnetic field.