Meaning: Completely mad. This is now commonly understood to mean crazy, although the original meaning is unclear and may have meant annoyed.
Origin: Mercury used to be used in the making of hats. This was known to have affected the nervous systems of hatters, causing them to tremble and appear insane. A neurotoxicologist correspondent informs me that "Mercury exposure can cause aggressiveness, mood swings, and anti-social behaviour.", so that derivation is certainly plausible - although there's only that circumstantial evidence to support it.
The use of mercury compounds in 19th century hat making and the resulting effects are well-established - mercury poisoning is still known today as 'Mad Hatter's disease'. That could be enough to convince us that this is the source of the phrase. The circumstantial evidence is rather against the millinery origin though and, beyond the fact that hatters often suffered trembling fits, there's little to link hat making to the coining of 'as mad as a hatter'.
The earliest known printed citation of the phrase that I know of is from Blackwood's Edinburgh Magazine, January-June 1829. It appears in a section of the magazine headed Noctes Ambrocianæ.