Completely mad. This is now commonly understood to mean crazy, although the original meaning is unclear and may have meant annoyed.
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æ. No. XL1V, in a fictional conversation between a group of characters that wouldn't have been out of place in Wonderland:
NORTH: Many years - I was Sultan of Bello for a long period, until dethroned by an act of the grossest injustice ; but I intend to expose the traitorous conspirators to the indignation of an outraged world.
TICKLER (aside to SHEPHERD.): He's raving.
SHEPHERD (to TICKLER.): Dementit.
ODOHERTY (to both.): Mad as a hatter. Hand me a segar.
The expression appears again (twice) soon afterwards, in a book by the Canadian author Thomas Haliburton - The clockmaker; or the sayings and doings of Samuel Slick of Slickville, 1835:
"And with that he turned right round, and sat down to his map and never said another word, lookin' as mad as a hatter the whole blessed time."
"Father he larfed out like any thing; I thought he would never stop - and sister Sall got right up and walked out of the room, as mad as a hatter. Says she, Sam, I do believe you are a born fool, I vow."
There's no explanation of the phrase in Haliburton's book to help us infer any sort of derivation - there's certainly no mention of poisoning or anything else to relate it to the practice of hat making.
There is also a suggestion that the phrase was originally 'as mad as an adder', i.e. a viper. That corresponds with the US expression 'as mad as a cut snake'. I can find no example of 'as mad as an adder' that predates the above citations though.
Another possible explanation is from New Zealand, in the name hatter that was given to miners who work alone. Writing in 1889, E. Wakefield, in New Zealand after 50 Years:
"Miners who work alone are called 'hatters', one explanation of the term being that they frequently go mad from the solitude of their claim away in the bush, exemplifying the proverb 'As mad as a hatter'."
That's more than fifty years after the first printed and so seems unlikely to be the origin. It's more likely that antipodean miners were called hatters because they were mad than the other way about.
Whilst not being the source of the phrase, we can't mention 'as mad as a hatter' and leave out Lewis Carroll. His 'Hatter' character from Alice's Adventures in Wonderland (1865) is of course the best-known mad hatter of them all. The Hatter is not actually described as mad in the story - merely a participant at 'a mad tea-party' - although he can hardly be called sane.
It would also be remiss to leave out the fact that mercury, which we now know to be highly toxic, was used in the manufacture of hats. Hatters commonly suffered from 'hatter's shakes', a form of nerve damage which gave symptoms similar to Parkinson's Disease and which is still known today as 'Mad Hatter's Syndrome'. A neurotoxicologist correspondent of mine has put forward the view that hatters could have been mad in either or both of the 'angry' or 'insane' senses. He states that "Mercury exposure can cause aggressiveness, mood swings, and anti-social behaviour. It is therefore likely that the mercury in hat making did lead to 'mad' hatters both in terms of rationality and plain old grumpiness."
Carroll may have taken his inspiration for the Mad Hatter from the known unusual behaviour of hatters and also from Theophilus Carter, who was an Oxford cabinet maker and furniture dealer with a reputation for eccentric behaviour. The cap, or in Carter's case the top hat, certainly fits. He was something of a 'mad inventor' and came up with the alarm-clock bed, which woke people by tipping the bed over. Carroll would have been familiar with the sight of Carter, in full top hat, outside his shop at 48 High Street, Oxford, where he lived in the 1850s - during the time that Carroll was an Oxford don.
Similes of the form 'as x as y' are extremely commonplace in English. They almost invariably link an object with a property that it is well-known to possess, e.g. 'as white as snow', 'as slippery as an eel' etc. Whoever coined this term would certainly have had reason to associate hatters with madness. Whether they meant hatters or adders and considered them to be annoyed or crazy, we don't know. Until we do, the derivation of 'as mad as a hatter' remains uncertain.