We investigate the generalized second law of thermodynamics (GSL) in generalized theories of gravity. We examine the total entropy evolution with time including the horizon entropy, the non-equilibrium entropy production, and the entropy of all matter, field and energy components. We derive a universal condition to protect the generalized second law and study its validity in different gravity theories. In Einstein gravity, (even in the phantom-dominated universe with a Schwarzschild black hole), Lovelock gravity, and braneworld gravity, we show that the condition to keep the GSL can always be satisfied. In $f(R)$ gravity and scalar-tensor gravity, the condition to protect the GSL can also hold because the gravity is always attractive and the effective Newton constant should be approximate constant satisfying the experimental bounds.