Sum rules for Jacobi matrices and their applications to spectral theory