Where did the expression "feeling blue" come from?
If you are sad and describe yourself as "feeling blue," you are using a phrase coined from a custom among many old deepwater sailing ships. If the ship lost the captain or any of the officers during its voyage, she would fly blue flags and have a blue band painted along her entire hull when returning to home port.
Also, see Origins of Navy Terminology for other expressions.
Since the most highly upvoted answer here seems to be based on dubious sources, and since no one else has as yet inquired into the origin of the phrase "feeling blue" (as opposed to the word blue in association with either "blues" or "blue devils"), I did some searching in Google Books for early examples of "feeling blue," "feel blue," "feels blue," and felt blue."
Early Google Books matches for 'feeling/feel/felt blue'
The earliest of the lot of matching phrases is from "A Thought or Two on Handkerchiefs," in The Monthly Traveler, or, Spirit of the Periodical Press (Boston: October 1835):
Some persons, it is true, are uncharitable enough to think the red noses in the community are owing to spirits of wine in some shape ; and the blue noses to spirits of wives and sweethearts ;—but I am not disposed to listen to any idea of the kind. If men drink, the member in question has no right to appropriate more than its share of good things taken in to the system ; and if men can't keep their wives' hands off their ears and noses they deserve to feel blue and black besides.
The earliest match for "feeling blue" appears in L. F. Apthorp, "Confessions of a Schoolmaster," in The Boston Book: Being Specimens of Metropolitan (1836):
Yes—I was attacked, literally, by a legion of live pork. The horrid circle contracted rapidly around me. Flight, in any sense of the word, was impossible. In this agonizing moment the clouds opened and discharged a tremendous shower of — dough-nuts. Henceforth let no melancholic victim of ennui complain of feeling blue, till he has felt the "pelting of the pitiless storm."
Another early instance of "feel blue" appears in "Our Country Forever!!!" in The Harrison Medal Minstrel (1840):
The TORIES full long have triumphant appeared,
But now they begin to feel blue,
For they know a tyrant has never yet dared,
To stand before Tippecanoe.
(Tippecanoe refers to General William Henry Harrison, hero of the Battle of Tippecanoe in 1811 and thirty years later the ninth president of the United States. He was the first President to be elected as a candidate of the Whig party, which originally arose in opposition to Andre Jackson [the seventh U.S. President], whom the Whigs viewed as a tyrant; hence the American Whigs' adoption of the name of the British political party opposed [at one point] to the King, and their characterizing Jackson's party [the Democrats] as Tories.)
And from Peter Pencil, "Stock-Jobbing in New York," in Graham's Magazine (September 1847):
Prices had reached their lowest point precisely at the moment that I had sold out mine, and instead of going down to ninety, as they would have done had I continued to hold, they "rallied," as the saying is, and rose to par. I looked and felt blue, and counted over my money again and again; I ciphered and calculated for half a morning, in endeavoring to make my loss less than it was. It was of no use, however, for the result of my counting and my ciphering were precisely the same, showing a deficiency of six hundred dollars and the brokerage.
Possible connections of 'feeling blue' to other 'blue'-related terms
Two instances suggest the possibility that "feeling blue" might have come from a longer phrase. From "Benedick's Lament," in Punch, or the London Charivari (April 1844):
Oh! the days when we were bachelors, a long time ago,
Were certainly the jolliest a man could ever know!
In happy independence we the cup of pleasure sipped,
And never knew what 'twas to feel blue-devilish or hypped:
No wife had we, nor squalling brats, nor anything so low,
In the days when we were bachelors, a long time ago!
And from James Burn, Commercial Enterprise and Social Progress: or, Gleanings, in London, Sheffield, Glasgow, and Dublin (1858):
Before the year 1847, if an Irishman was a few days without joining in a "row," he would say that he was feeling "blue-moulded for the want of a bating [that is, 'a beating']." Since the above date, a considerable change has come over the national mind. Father Matthew, the failure of the potatoes, and a more reflective condition of mind, have made the people care less for poteen [illicitly distilled whiskey], and more for themselves. The consequence of which is, that Ireland is, at the present time, the most sober division of the United Kingdom.
"Blue-moulded" is not a term I'm familiar with, but Statistical Survey of the County of Antrim (1812) indicates that it refers to an affliction of wheat that may occur when the grain is improperly threshed:
Threshing is not approved of unless the grain is very dry ; when that is not the case, it is bruised on the barn floor, and grows blue-moulded.
Th connection of "feel blue" to "blues" is explicit in "A Discourse by President Heber C. Kimball, Delivered to the Tabernacle, Great Salt Lake City, April 2, 1854," in Journal of Discourses by Brigham Young, volume 2 (1855):
Now suppose my wives and my children would take the same course to please me, and be subject to me, as I am to brother Brigham [Young], would there be any sorrow, or confusion, or broils? No, there would be no sorrow, there would be no blues in my family. I am never blue when I do brother Brigham's will ; but when I do not do it, I begin to grow blue ; and when brother Brigham does not do the will of God, he begins to feel blue. It always makes my family feel blue when they will not do as I wish them ; and I suppose it affects almost every family so in this town.
"Feeling blue" and "feel blue" begin to appear in Google Books publications from the 1830s. It seems highly likely that the wording arose naturally from earlier slang terms involving the word blue. Here are two early (and potentially relevant) entries from Francis Grose, A Classical Dictionary of the Vulgar Tongue (1785):
BLUE, to look blue; to be confounded, terrified, or disappointed.
BLUE DEVILS, low spirits.
John Barrett, Dictionary of Americanisms, first edition (1848) shows a similar range of meanings in U.S. English slang usage:
BLUE. Gloomy, severe; extreme, ultra. ...
BLUE DEVILS. To have the blue devils is to be dispirited.
Either term could reasonably be interpreted as providing the immediate referent for early use of the phrase "feeling blue."
According to the following Wikipedia entry (You can follow the references therein and find out more about this expression)...
This is because blue was related to rain, or storms, and in Greek mythology, the god Zeus would make rain when he was sad (crying), and a storm when he was angry. Kyanos was a name used in Ancient Greek to refer to dark blue tile (in English it means blue-green or cyan). The phrase "feeling blue" is linked also to a custom among many old deepwater sailing ships. If the ship lost the captain or any of the officers during its voyage, she would fly blue flags and have a blue band painted along her entire hull when returning to home port.
A small excerpt from Metaphorik.de goes like this...
The proximity of dark blue to black on the colour scale, and its historical grouping with dark colours, may have contributed to blue’s links to depression and to fear – to have the blues, or to feel blue.
The original root is unknown, however etymonline.com says of the related blues:
meaning "depression, low spirits" goes back to 1741, from adjectival blue "low-spirited," late 14c.