Relativistic dynamics of nonideal fluids: Viscous and heat-conducting fluids. I. General aspects and 3+1 formulation for nuclear collisions