Gravitational lensing occurs when the gravity of a massive object, like a galaxy, warps the fabric of spacetime, bending the light from a more distant background object.