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 and Elephind for early examples of "feeling blue," "feel blue," "feels blue," and felt blue."
Early matches for 'feeling/feel/feels/felt blue'
The earliest of the lot of matching phrases is from "Confessions of a Country School Master," in Boston [Massachusetts] Monthly Magazine (June 1826), in the midst of a description of an unusual dream:
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. Hencefooth let no melancholic victim of ennui, complain of feeling blue, till he has felt the "pelting of the pitiless storm." Every nut seemed to strike like the ball of a nine-pounder [cannon]. I was reduced to paste in a twinkling.
From an untitled item in the [Lawrenceburg] Indiana Palladium (November 24, 1832), reprinted from the Boston [Massachusetts] Morning Post of unspecified date:
The Winter, with a proper respect for our nerves and noses, has saluted us for a few days past to the tune of 30 to 36 Fahrenheit, but, thanks to the heats of political contests, nobody has thought of shivering or turning blue but the minority; and as none acknowledge themselves in the minority, of course no one feels blue, except a few the Police court have sent to Fuddle Island to recover their carnation.
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.
From "Our Country Forever!!!" in The Harrison Medal Minstrel (Philadelphia, Pennsylvania: 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. A variant of this political verse, printed in the Vincennes [Indiana] Gazette (August 1, 1840) as "The People Are Coming" and to be sung to the tune of "The Campbells Are Coming," uses this wording:
The People are coming, hurrah, hurrah,
The People are coming, hurrah, hurrah,
No wonder the spoilers begin to feel blue,
For all are hurrahing for Tippecanoe.
From "Letter for Lahaina" in the [Honolulu, Hawaii] Polynesian (August 22, 1846):
Week before last when somebody went [to California] everybody were there to see him off, but not being informed, I missed it. There was a perceptible rise in onions, and eggs have since turned out chickens. Beef in a sudden fit of self esteem, values itself at 11 cts. per. lb., which makes house keepers feel blue—a color in which the milk fully sympathises.
From Mrs. C.H. Butler, "The Blue Devils," in The Columbian Magazine (New York: April 1847):
Said he, "I am the Prince of the Blue Devils, and these are my subordinate imps. This is the first time we have ever presumed to appear in your presence, nor should we now were our visit a professional one. ... This paper," he continued, drawing forth from a miniature saddle-bag a bluish parchment. "this paper we have to request that you will deliver into the hands of our greatest enemy—of that rash mortal who dares to defy our whole race, and who, unless he complies with the demands contained therein, shall not only be made to look but to feel blue! ..."
And from Peter Pencil, "Stock-Jobbing in New York," in Graham's Magazine (Philadelphia: 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.
Google Books and Elephind report instances of "feeling blue" and "feel blue" in publications from as early as 1826. 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." The second earliest in-the-wild match (from 1832) that I found seems to play on the connection between blue from "blue devils" and blue from "turning blue" with cold.
As for the place of origin of the phrase "feeling blue," it is striking that the earliest instances of "feeling blue" (from 1826), "feels blue" (from 1832), and "feel blue" (1835) are from the same city (Boston, Massachusetts). Nevertheless, that tiny sample of evidence is hardly sufficient to support a firm claim that Boston is the cradle of "feeling blue." A stronger case can be made that the wording is of U.S. origin, considering that virtually all of the earliest matches come from U.S. states or territories. (The lone instance before 1850 from Britain is from 1844 and appears in the form "feel blue-devilish.")
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.