Molecular cloud evolution – IV. Magnetic fields, ambipolar diffusion and the star formation efficiency