Earthquakes occur along fault lines, or cracks in the earth's crust.
The Earth's crust is split into tectonic plates that constantly shift around. The borders between tectonic plates, fault lines, are loaded with pressure as they move and grind against each other, so when the pressure is released, the plates will suddenly shift with such a force that a massive shockwave will disperse throughout the crust. This is how an earthquake occurs.