Gravity and the thermodynamics of horizons