In HHGG, it is explained that the giant program that the earth was part of was messed up at the very end and gave the qrong question to the answer "42" because of that.
Wiki: "The program on the "Earth computer" should have run correctly, but the unexpected arrival of the Golgafrinchans on prehistoric Earth caused input errors into the system—computing (because of the garbage in, garbage out rule) the wrong question—the question in Arthur's subconscious being invalid all along."