I don't have a clear answer about the origin of "so long" in the sense of "good-bye" or "au revoir," but I did find some striking early discussions of the expression. From a list of student slang expressions categorized as "Arbitrary or Unexplained Coinages: Origin Unknown," in Willard Gore, "Student Slang" (1895):
so long. Good-bye; au revoir.
...
So long, in the sense of good-bye, or au revoir (p. 4). An interesting discussion of this phrase appears in Notes and Queries, 6th Series, Vols. II., III. On p. 67 of Vol. II., 'B. P.' says that the expression is fully seventy-five years old; it is used by sailors, by uneducated people in the United States, and by colored people of the Middle States. On p. 194, T. C. McMichael remarks that it is heard frequently in Liverpool, especially amongst the seafaring class; it probably derived from the East Indian word salaam, used as a salutation almost universally in some parts of the East. On p. 195 B. D. Moseley says that it is an absurd rendering of the French saluons. {Cf. the low comedian's rendering of au revoir, by 'over the river' or olive oil'.} On p. 496 'A West Coaster' makes objection to these derivations on the ground that salaam and saluons are expressions of greeting,'so long', one of leave taking. 'So long' means 'so long as we do not meet'. In Vol. III., p. 18, Augustus Weisbacher notes that 'so long' is common amongst English and Dutch in Grahamston, South Africa.
For a literary use of the expression see Walt Whitman's Leaves of Grass (Phil., 1884), p. 380: A poem entitled "So Long."
The poem headed "So Long!" appears in a section of Leaves of Grass called "Songs of Parting." It does not appear in the 1856 edition of Leaves of Grass, but it does appear in an edition published in 1860. Not surprisingly, "So Long" uses the expression "so long" multiple times:
To conclude—I announce what comes after me, / The thought must be promulged, that all I know at any time suffices for that time only—not subsequent time; / I announce greater offspring, orators, days, and then depart.
...
I have pressed through in my own right, / I have offered my style to every one—I have journeyed with confident step, / While my pleasure is yet at the full, I whisper, So long, / And take the young woman's hand, and the young man's hand for the last time.
...
So long! / I announce natural persons to arise, / I announce justice triumphant, / I announce uncompromising liberty and equality, / I announce the justification of candor, and the justification of pride.
So long! I announce a man and a woman coming—perhaps you are the one / I announce a great individual, fluid as Nature, chaste, affectionate, compassionate, fully armed. / So long! I announce a life that shall be copious, vehement, spiritual, bold, / And I announce an old age that shall lightly and joyfully meet its translation.
...
Dear friend, whoever you are, here, take this kiss, /I give it especially to you—Do not forget me, / I feel like one who has done his work—I progress on,— / (long enough have I dallied with Life,) / The unknown sphere, more real than I dreamed, more direct, darts awakening rays about me—So long! / Remember my words—I love you—I depart from materials, / I am as one disembodied, triumphant, dead.
At least some of these instances of the phrase—in the context of the poem, which is the last on in this edition of the book—clearly allude to the meaning "farewell." This is five years earlier than the earliest example of "so long" in the sense of "goodbye" that the OED cites, according to Barrie England's answer. Whitman was born and raised in Long Island, New York, and spent much of the first half of his life there.
In the context of Whitman's usage it is amusing to dive into the discussion of "so long" that runs across multiple numbers of Notes and Queries, starting with this one from July 24, 1880:
"So LONG."—This is a queer expression, used in the sense of "good-bye," often heard int he United States, but always by uneducated people. Sailors, on bidding you good day, say "So long'" Coloured people in the Middle States employ these words. It is not of recent adaptation, being fully seventy-five years old. Is there any word or combination of words sounding like so long, meaning "good-bye" in use on the African coast or in the East Indies? Do English sailors use these words? [signed,] B. P.
The first respondents to B.P.'s query were Thomas McMichael of Liverpool an B.D. Moseley of Burslem, in the September 4, 1880 issue of Notes and Queries. First McMichael:
This expression may very frequently be beard in this city, especially amongst the seafaring class, and it is interesting to learn that it is in common use in various parts of the United States. The result of some thought and inquiry convinces me that this expression takes its origin from the East Indian word "salaam," used as a salutation almost universally in some parts of the East, hence its use in the corrupted form of "So long" is principally by sailors who have heard it in those countries, and, sailor-like, are not very particular as to its correct pronunciation. By-the-bye, is not this word "salaam" another form of the Hebrew "salem," meaning peace? I should be glad if any reader could enlighten me on this point.
I will just remark, in conclusion, that several other explanations of "So long" have been suggested to me ; but although some of them are plausible, yet none of them will bear comparison with the foregoing.
And second from Moseley:
This is an absurd rendering of the French "saluons," from a fancied resemblance in sound to the English expression "So long," pronounced as one word. A similar affectation, now much in vogue amongst our gommeux, is th rendering of au revoir with "olive oil."
Next from "A West Coaster" in the December 18, 1880 issue of Notes and Queries:
Since the appearance of the replies of Messrs. T. C. McMichael and Moseley in your number of the 4th Sept. I have waited to see if some known authority would not set them right. Salaam and Saluons are words of greeting, the expression "So long" one of leave-taking. Were either of your two correspondents dropped down among the English residents of any Spanish-American city, from Montevideo southward to Panama northward on the opposite coast, he would find that he could not fail to use this form on leaving his guest or acquaintance on every possible occasion without risk of being deemed unmannerly or unobservant. The Spaniards, always known as a punctilious race, never leave one another without this polite ceremony : and their expressions have nothing to do with health. "So long" means "So long as we do not meet." But the polite Spaniard goes further, and says how long that will probably be. Hence the phrases"Hasta ahora," "Hasta luego," "Hasta mas tarde," "Hasta despues," "Hasta la vista" (Au revoir), and finally "Adios." Could either of your correspondents suggest a more convenient form of rendering all these gradations than that adopted by the scions of English and Spanish parents—the indefinite "So long"? Sailors who have never used the phrase before acquire it on their arrival on the Spanish-American coasts ; and, once learnt, the custom is never forgotten. West coast folks in England always use it to one another as a pleasing custom, but we are chary as to employing it in purely English circles.
And from Augustus Weisbecker of Grahamstown, South Africa, in the January 1, 1881 issue of Notes and Queries:
This phrase is a common salutation in this colony amongst the English and Dutch, and used on a temporary separation of friends, as au revoir by the French. I remember hearing it amongst the Blue Noses of Nova Scotia and the New Brunswickers.
The expression also appears in Frederick Jago, "The Ancient Language, and the Dialect of Cornwall, with an Enlarged Glossary of Cornish Provincial Words" (1882):
So-long! Good bye! Adieu! (Heard in Looe and Waldebridge). W.T.A.P[attison, "of Duporth, near St. Austell"]
Perhaps the most intriguing early example of "so long" that I came across was from a translation of a story by the German writer E.T.A. Hoffmann, "The Mines of Falun," in Graham's Illustrated Magazine (January 1858):
The wedding-day at length approached. It was St. John's day. Early in the morning Elias knocked at his bride's door. She opened it, and drew back affrighted when she beheld Elias in his wedding suit, pale as a corpse, and with sunken, gleaming eyes.
"I will confide in you my dearest Ulla," he said, in a feeble, trembling voice, "that we are now on the eve of the highest happiness of which mortals are capable. Every thing was revealed to me last night. Below in the mines in the granite and mica lies a bright red, sparkling ruby, on which our horoscope is engraved. It is finer than the ruddiest carbuncle ; and when we are united in true love, and gaze into its clear light, we shall see plainly how wonderfully our hearts are interwoven with the strange sprig that grows from the heart of the queen in the centre of the earth. It only needs that I should bring that ,jewel up to the daylight, and that, moreover, I will do. For so long, farewell, beloved Ulla, I shall soon return."
A similar phrasing appears in a translation of Fritz Reuter, Seed-Time and Harvest; or, During My Apprenticeship, serialized in The Living Age (January 1, 1871):
"But"—here he drew out from his pocket an immense double-cased watch, such a thing as one calls a warming-pan—"really, it is close upon seven! I must hurry, for my people need looking after."
"Hold on," said Habermann, "I will go part way with you. Good-bye for so long, Jochen."
"Good-bye, also, brother-in-law," said Jochen, and remained sitting in his corner.
It thus seems possible that "So long" may represent a telescoped form of the longer phrase "Goodbye for so long [as I do not see you]"—or in other words, "Goodbye until I see you again."