Magnetic fields in diffuse HI and molecular clouds