The formation of molecular hydrogen in the interstellar medium