Magnetic fields in M dwarfs from the CARMENES survey