It is mostly just a joke.
There are numerous ways to explain it though.
There is a suggestion at the end of The Restaurant at the End of the Universe that the Ultimate Question is "What do you get when you multiply six by nine?"
So, 6 x 9 = 42.
Which of course is incorrect.
The idea is that the universe is based upon a mathematical nonsense, with the end result that nothing really makes sense.
That of course is not my personal view - but I do find it funny in the context of the books.
Interestingly, in Base 13... 6 x 9 does in fact = 42.
But that assumes you understand Bases. If you don't I can go into it, but it's pretty unimportant.
Adams said himself that "People don't write jokes in base thirteen." Which pretty much killed off that theory.
There are other possible questions...
"How many roads must a man walk down?"
or, predictably,
"What is the answer to six times seven?"
However, getting onto that which is most interesting of all. There is a website dedicated to everything connected to the number 42. There are some shocking applications of this number. Things to do with the DNA of humans... and mice... amongst other things.
I do not have the link to the website any more - so happy hunting for it.
Interestingly also, 42 appears in the Bible a few times.
Matthew records 42 generations from Abraham to Christ.
There are also two sets of 42 months mentioned in Revelation. 42 months being exaclty half of seven years. Seven years being a significant measure of time in the Bible.
Adams insisted though that he just thought it would suit, for no real reason, and put it in the book. Well, there you are...