Evolution of gas-phase metallicity across cosmic time