Etymology of "count your blessings"
Solution 1:
To bless properly means to bestow favor on something, with a strong implication of divine favor and luck. Blessing is a straightforward verbal noun derived from that, and it indicates those things which one has received from others without deserving it, and again has a strong connotation of those things acquired by good luck or providence. So the phrase count your blessings is an invitation to reconsider all of the things that you have which are going well, all of the ways in which you have been blessed, rather than focusing on the negative aspects of your life.
(The phrase "God bless you" following a sneeze is literally an invocation for God to bless the person who has sneezed, i.e. to protect them from disease and demonic influence. However, the phrase has become completely conventionalized, so a person saying it does not necessarily believe in God or demons.)
Solution 2:
My Rabbi told me an answer many years ago. I don’t know if it is true or not, but I will relate it.
He said: “Do you know where the phrase 'count your blessings' comes from? The ancient Jewish tradition is to try to get to 100 blessings a day. Hence you count your blessings to see if you can find 100 of them by the end of the day. It explains why there are so many blessings that a traditional Jewish person will say throughout the day. It’s not just 'grace' after the meal. It’s just everything you do in a day – even blessings for after successfully going potty. Thank goodness my body is working correctly right now…”
The idea is to focus as much as possible on all the things that are going right in your life. When you do that, you will find you are a happier person.
Solution 3:
Before 'count [one's] blessings'
A foreshadowing of "count [one's] blessings may be found in the Bible, in Psalm 40, verse 5:
You have multiplied, O Lord my God, your wondrous deeds and your thoughts toward us; none can compare with you.
Were I to proclaim and tell of them, they would be more than can be counted.
These "wondrous deeds" and "thoughts toward us," coming as they do from God, may well be interpreted as blessings; but the speaker concedes that they are uncountably many, which makes this passage a not entirely satisfactory source for the idea of "counting one's blessings." An interesting consideration of God's innumerable blessings (described, however, as "mercies") appears in an oddly belligerent sermon by Thomas Halyburton, "A Discovery of Man's Natural State ; or, The Guilty Sinner Convicted" (by 1712):
You have sinned against the God of your mercies, the God who ha loaded you with favours. O sad requital you have given to God for all the kindnesses he has done to you, since the morning of your day! ... It is not one mercy, or two, but innumerable mercies, innumerable kindnesses. Reckon, O sinners! what the mercies of God are, if you can. Nay, if ye can count the stars in the heaven, or the sand of the sea-shore, you may. David says in that 71st Psalm, "That he knows not the number of God's salvation;" and who may not say with him in this? God every day preserves you from many thousands of inconveniences that would destroy you, and bestows upon you many thousands of mercies. He loads you with his benefits, and ye load yourselves with your sins against him.
Indeed, the Bible-endorsed notion that the blessings of God are uncountably many may have acted as a force against the emergence of "count [one's] blessings" as a popular saying, since at some level the saying posits that such blessings are numerically finite.
The emergence of 'count [one's] blessings' as a popular phrase
The earliest instance of a reference to counting one's blessings that I've been able to find is from Harriet Martineau, A Tale of the Tyne (1833), where it appears as part of a comparison:
In ten minutes more, Mr. Severn left her [Mrs. Eldred], fully convinced that it would be much easier to count her troubles than her blessings; that Providence has a wise and kind purpose in all that it inflicts; and that the best welcome she could offer her husband on his return would be the sight of what she had done in his absence for his sake.
The exact wording "count your blessings" first appears in Google Books search results in Old Alan Gray, "A Cheerful Chapter," in The Bible Class Magazine (May 1851):
The merry group [of country girls dancing round a Maypole] set my heart a dancing, and as I played the part of a spectator, I gave the young people my blessing. May-day is now come round again, and willingly would I have you as cheerful and as happy as those light-hearted girls, Why not! Surely when nature is rejoicing around you, you would not carry a face of gloom. In such a course the sunny sky will bear witness against you; the trees will lift up their green-leaved boughs against you; and the glowing flowers will rise up in your path to reproach you. Be not not behindhand to celebrate the general jubilee. Count your blessings, reckon up your mercies, so that you may mingle your note of thankfulness with those of the rejoicing spring. Let May-day be a day of happiness and praise.
And similarly from "The Art of Being Happy," in the [Dowagiac, Michigan] Cass County Republican (January 6, 1859):
To all such [unhappy people] we should say, study he art of being happy. Begin at once and take a better view of life. Count up your blessings. Enumerate your friends and compare them with the number of your enemies compare your condition with thousands who have no comforts, friends or prospects. See whether your troubles are not all borrowed; or whether they do not all come from false opinions or feelings, vanity, pride or selfishness. People can generally be happy if they try. Keep a sunny hart and life will be sunny.
Was 'count your blessings' a reaction to 'count my sorrows [or troubles]'?
It is possible that "count your blessings" arose as a rejoinder to a then-famous poem by Cuthbert Shaw, "Evening Address to a Nightingale" (by 1770), in which the poet writes
But O! for ME in vain may seasons roll,
Nought can dry up the fountain of my tears;
Deploring still the COMFORT OF MY SOUL,
I count my sorrows by encreasing years.
This poem appeared in numerous collections of poetry between 1770 and 1850. James Henry, "Dirge for the 13 December 1852" in My Book (1853) adopts a similarly dismal tone:
Eleven — 's the turret's awful cry:
To count my sorrows let me try;
False friends, vain hopes, declining age;
O ! lay me in some hermitage,
Far from the world's discordant jars,
Beyond its envies, feuds, and wars;
Beyond the bigot sectaries' reach,
Who, when they ought to practice, preach.
And Juan Van Halen, Narrative of Don Juan Van Halen's Imprisonment in the Dungeons of the Inquisition at Madrid, and His Escape in 1817 and 1818 (1828) has this:
During the short time that we remained alone, he aid to me ; "This, Sir, is but a trifling affair. There are few of us who have not undergone troubles of some kind or other. Even myself, though you see me now in this uniform, had in the folly of my youth become monk of San Juan de Dios, of which I soon repented. On leaving the convent, I made the last campaign against the French, and could count my troubles by the dozen."
These published instances of sorrow and trouble counting precede the published instances of blessings counting, but that chronology doesn't establish a causal relationship between the two. I'm inclined to think that "counting [one's] sorrows/troubles/woes" was never so far advanced toward becoming a popular expression as to call forth "count [one's] blessings" in response.
The part played by Johnson Oatman's hymn, 'Count Your Blessings'
Captain Claptrap's answer mentions a very popular hymn by Johnson Oatman, Jr. titled "Count Your Blessings" (1897), which has this chorus:
Count your blessings, Name them one by one,
Count your blessings, See what God hath done;
Count your blessings, Name them one by one,
Count your many blessings, see what God hath done.
Max Cryer, Common Phrases: And the Amazing Stories Behind Them (2010) asserts that Oatman's hymn was a decisive factor in the popularization of the phrase "count your blessings":
Count your blessings
The phrase has become familiar mainly because of Irving Berlin's song "Count Your Blessings" (instead of sheep) from the movie White Christmas (1954) and the recordings of Bing Crosby, Rosemary Clooney, and Eddie Fisher.
But the line had first been heard in the hymn originally called "When Upon Life's Billow" by Johnson Oatman Jr., published in 1897, but later known as "Count Your Blessings"—for obvious reasons[.]
By 1897, however, dozens of instances of "count your blessings" as reassuring (or dismissive) advice to the suffering, the oppressed, the despairing, the aggrieved, the grumbling, the whiny, and the spiritually doubting had already appeared. Two famous writers to use the phrase prior to the 1890s were John Ruskin and Mary Mapes Dodge.
From Ruskin's "Plaited Thorns," in Fors Clavigera: Letters to the Workmen and Labourers of Great Britain (1875):
I will first count my blessings (as a not unwise friend once recommended me to do, continually ; whereas I have a bad trick of always numbering the thorns in my fingers, and not the bones in them).
From Dodge's Donald and Dorothy (1881), serialized in St. Nicholas (May 1882):
"It wont do any good to fret about it, you know, Miss Dorry. Come, now, you'll have the awfulest headache that ever was, if you don't brighten up. When you're in trouble, count your blessings—that's what I always say, and you've a big share of 'em, after all, dear. Let me make you a nice warm cup of tea—that'll build you up, Miss Dorry. It always helps me when I—Sakes! what's that?"
Given the many prior instances of the expression, it seems likely that that Oatman's hymn served to popularize the expression further, not to introduce it to the English-speaking public at large.