An earthquake occurs when energy is suddenly released in the earth's lithosphere: the surface of the earth shakes because of this energy. When this energy is released, it creates seismic waves.
Earthquakes can occur anywhere within Earth's crust, but most commonly occur along active plate boundaries. Earthquakes that occur intraplate (within plates) are usually caused by the reactivation of very old fault systems. Earthquakes can even be caused by human activities such as nuclear testing.
Diagram of intraplate earthquake occurrence:
Diagram of plate boundary earthquake occurrence: