The precise source of this popular
phrase is actually in question, although it certainly was already in
widespread use by the time
Alice in Wonderland was published in 1865. Personally, I agree with
thefez and feel the term derives from the fact that hatters were driven
mad because of poisoning by the
mercurous oxide used during the
felt tanning process. Others contend that it derives from "
mad as an adder," meaning "as dangerous as a snake". It's quite possible that this phrase was first used in reference a particular
mad hatter, 17th-century Englishman Robert Crab.
If you're interested in seeing pre-Alice uses of the phrase, you can find it in Thomas Haliburton's The Clockmaker (1836) and William Makepeace Thackeray's Pendennis (1850).