Evan Morris over at The Word Detective, answering a similar query, has some helpful musings.
He argues that despite all the good lemons have done, they've suffered from an image problem since the dawn of their cultivation—due primarily to their stinging acidity and tough skins.
The word “lemon” comes to us from the Old French “limon,” which was derived from Arabic roots and served as a generic term for citrus fruit in general (which explains how the same root could also give us “lime”). The use of “lemon” to mean “disappointing result” or “something unwanted” is very old, reflecting the fact that, while useful in cooking, a lemon standing alone is just a lump of sourness with a tough skin to boot. In Shakespeare’s play Love’s Labours Lost (1598), for instance, one character proclaims, “The armipotent Mars, of lances the almighty, Gave Hector a gift …,” to which another puckishly suggests, “A lemon.”
And, clearly drawing from some of the OED citations mentioned by @Barrie, he concludes,
In the mid-19th century, “lemon” was used as a colloquial term for a person of a “tart” disposition, as well as, more significantly for our purposes, slang for a “sucker” or “loser,” a dim person easily taken advantage of. It has been suggested that this latter use stems from the idea that it is easy to “suck or squeeze the juice out of” such a person (“I don’t know why it is, rich men’s sons are always the worst lemons in creation,” P.G. Wodehouse, 1931). By 1909, “lemon” was also firmly established in American slang as a term for “something worthless,” especially a broken or useless item fobbed off on an unsuspecting customer.
It’s likely that the current use of “lemon” to mean “something that doesn’t live up to its billing” or “a disappointing purchase” comes from a combination of “lemon” in the “sucker” sense (i.e., the buyer got “taken”) and the much older sense of “lemon” meaning “something undesirable.”
Also of note, I found occasional use of the phrase (at least as early as 1918), "to give someone a lemon and pass it off as a nugget (of gold)." If this was the original saying, later shortened to "handing someone a lemon," then the implication of trickery is confirmed and the metaphorical use of lemon further explained.