The original idiomatic use of 'stay the course'
As phoog mentions in a comment above, the earliest instances of "stay the course" meant "to halt or temporarily impede a process." Thus, Ben Jonson, Volpone, or The Fox (1605) has this line:
Avocatore 1: Can you plead ought [that is, aught] to stay the course of justice? If you can, speak.
From "Certain Observations touching the two great Offices of the Seneschalsey or High-Stewardship, and High Constableship of England" (1641), in A Collection of Scarce and Valuable Tracts on the Most Entertaining Subjects, volume 5 (1811):
It is to be understood notwithstanding, that none may stop the course of any ordinary running streames within his banks or ditches longer then from the rising to the going down of the sunne. Neither may any one stay the course of waters with any new ponds, ditches, or sluces, but shall permit them to run their course, lest the mills standing upon their streams, or men in their affairs, tanners, diers, and such like, should thereby suffer losse and detriment. If any shall stay them for the filling up of pools, or ponds, they shall be bound to restore such losses as the milners, or others living by the passage of those waters, shall have sustained by the with-holding of them, and waters shall be suffered to run their accustomed course.
Two examples from the early nineteenth century involve ships and their progress through the sea. From Thomas Maurice, Indian Antiquities (1812):
By them [the Phoenicians], the ancient sails, which, in many instances, were made of nothing but hides, sewed together, were exchanged for more flexible ones of linen, and the leather thongs, or cords, used for bracing them and various other purposes, for others of hemp and flax. By them, too, the old clumsy anchors, which sometimes consisted only of a large stone, and sometimes of a log of wood, with a quantity of lead affixed, or a bushel of sand, let down to stay the course of the ship, were displaced for anchors of iron having at fist one, and afterwards two, teeth, or flukes.
And from Richard Pering, A Treatise on the Anchor ... and a Schedule of Proportionate Weights of Anchors (1819):
The most ancient anchors, we are told, were made of stone, and sometimes of a crooked piece of wood, to which lead was attached to make it sink, and to stay the course of the ship. These are not altogether thrown aside by the Chinese, in mooring their junks, even to this day. Afterwards anchors were constructed of iron, and furnished with flukes.
All the pretty horses
The use of "stay the course" in the very different sense of a horse completing a race appears for the first time in Google Books search results going back to 1885, in a citation in Farmer & Henley Slang and Its Analogues, volume 6 (1903):
STAY, ... Verb. (colloquial).—To endure, last out, or persevere : as an athlete in exercise, a horse in racing, an author in public favour. Hence STAYER = anybody or anything capable of holding on for a long time ; STAYING-POWER = capacity for endurance.
- D[aily] Tel[egraph], 14 Sep. He won at Lincoln ... and would STAY better than Pizarro. Ibid., 11 Nov. Doubts are also entertained concerning her ability to STAY the course.
By 1902, the phrase "stay the course" seems to have become thoroughly established in horse-racing lingo, but to have broadened to mean something like "compete successfully and finish well. From "The World of Sport," in The Sketch: A Journal of Art and Actuality (August 27, 1902):
The Irish colt, St. Brendan, is doing well in his work. He has good speed, and is certain to stay the course. The men of observation at Newmarket fancy Royal Lancer has a big chance. But he has been more or less under suspicion, and he is hardly likely to stay the course in a strong run race.
'Don't halt the course; hold to it!'
This was followed surprisingly quickly by yet another sense of "stay the course"—one very nearly diametrically opposed to the sense that prevailed between 1600 and the late 1800s: to continue on a particular course of action regardless of unexpected setbacks encountered along the way. The first instance appears to be in "Military Service Bill, Second Reading" (4 May 1916), in The Parliamentary Debates (official Report): House of Commons, third volume of Session 1916 (1916), which rather oddly shifts into that sense of "stay the course" from the racing sense:
Mr. HOLT: ... The policy of the large Army involves the policy of the knockout blow. It means that if you are going to have a large Army you have got to use it at once, and you have got to bring the War to a speedy termination, and the question which we have got to ask ourselves is whether, if we did this, we should be able to stay the course? ... I have always thought this was going to be a long war, and I want to be sure that we can stay the course. It has always seemed to me that when we started this War we were in the position of an athlete who had made the three mile race his special study, and who was suddenly asked to run on a quarter of mile course. The policy of shortening the course is a very dangerous one, and we ought to be quite satisfied that we can stay the course. I do not know what the present Chancellor of the Exchequer thinks of it, but I should have thought that anybody who knows, what is common property, the financial position of our friends, must be perfectly well aware that there is not the remotest chance of our being able to stay the course until the year 1918. We are not likely to stay the course until 1918 at out present rate of expenditure. Let us be satisfied on this subject.
And in Minutes Taken Before the Royal Commission on the Civil Service (1929–31) (31 January 1930) [combined snippets]:
[6674.] ... The principle seems to be this really, that all persons who pass the tests, other than the health tests, should be admitted to the Service, and the only differentiation against bad lives, apart from the sick-leave and so on, is that unless they stay the course they would not get a pension?
—Yes, but you see the difference between temporary personnel and outsiders who would come in under normal open recruitment is that the Departments have had an opportunity of testing the health records of the temporary staff. Those who have been in the service for 7 years would not be retained unless their health records were good.
6675. What you really mean is this: If you have a temporary who has been 7 or 8 years in the Service, it may be that on the first entry the health was unimpeachable. Where such people have spent 7 or 8 years in the Service as temporaries; you are suggesting that in the special circumstances of that case they ought to be regarded as persons who had passed in with a health certificate 7 or 8 years ago, at any rate to this extent, that if they stay the course, they should get a pension?
Here the notion of "stay the course" means (as it seems to have done in the speech about financing World War I) "sticking it out" or "seeing it through"—the modern meaning of the phrase.
But instances of the old usage continue to occur at least as late as 1980. For example, from Maurice Collis, Foreign Mud: Being an Account of the Opium Imbroglio at Canton in the 1830's (1946):
The [Chinese] Central Government was concerned to extirpate the whole traffic, not merely the riverine carriage of opium. His [Captain Elliot's] order for that to cease was only a palliative and one wholly insufficient to stay the course of events. It pleased no one; the Chinese were unsatisfied, and the British merchants felt that a Crown authority, by taking a stand against opium, though only in part, thereby admitted the wrongfulness of the whole, an admission which might be highly embarrassing in the future if, as was rumoured, the [Chinese] Court was determined to go to extremes.
And from Henry Dodwell, The Founder of Modern Egypt: A Study of Muhammad Ali (1980):
Near Tripolitza [in 1821] 3000 Greeks defeated 5000 Turks. The result was the surrender of that place and Navarino. At neither did the insurgents carry out the terms of the capitulation, and at Tripolitza they slaughtered 8000 Muslim men, women, and children. As a matter of course these events were followed bu like massacres of Greeks at Constantinople and elsewhere. The Greek patriarch and four of his bishops were hanged, and it was believed that at least one Greek life was taken for every Muslim that had perished in the Morea. The Shaikh-ul-Islam—the head of the theologians of Constantinople—was removed from his post for his unworthy conduct in seeking to stay the course of this revenge.
Interestingly, China Notes, volumes 14–23, pages 55 & 89 (1979[?]) [combined snippets] uses the phrase twice, in opposing senses, within 35 pages of one another:
But exorcizing the ghost of Mao has been a bitterly fought issue. Teng's drive for efficiency and modernization is cutting to the heart of the system the system Mao crafted in the 1960s and 1970s. This threatens some Chinese, bewilders others, and makes many cadres afraid to implement these bold initiatives until they are certain that the new program will survive its 74-year-old author. Teng believes that only direct repudiation of Mao's legacy of political radicalism will make these administrators confident that Teng's successors on the Politburo will stay the course, yet without their active support, the controversial new program will falter.
We have not realized that the violence of the revolutionary movement is but a reaction and a result of the seen and unseen violence of the present regime, and the reason why this revolutionary movement exists and has spread is that we have not done our duty in the reform of society, but rather have obstructed it and tried to stay the course of history, thus making Christianity into a tool of the reactionary forces in the world today.
The triumph of holding steady
In the United States, the decisive moment when the public embraced the (relatively) new meaning of "stay the course" and abandoned the old one seems to have occurred in 1982. In the off-year Congressional election (a federal election during a year when the Presidency was not being contested) in 1982, President Reagan, two years into his first term in office, made "Stay the Course" his rallying cry for electing Republicans to Congress to support his economic policies, which had run into some turbulence. The last gasp of the old understanding of "stay the course" came in this rather airily delivered note in Verbatim, volume 9 (1982) [combined snippets]:
When President Reagan exhorted Senators and Congressmen to stay the course, the actual meaning of his words was the opposite of his intended meaning. What President Reagan intended to say—and the American public no doubt understood him to mean—was that Congress should remain steady on the course it had set for itself. But what he actually said was that Congress should halt its course. For the transitive verb stay means stop, postpone or delay,' as in the locution to stay the execution.
The intransitive verb stay does carry the meaning President Reagan intended but like its synonym remain, it must be followed by an adverbial, not a direct object. No one would use remain as a transitive verb (e.g., "to remain the course"). To convey his intended meaning, President Reagan would have had to tell Congress to "stay on the course," just as he would have had to say "remain on the course."
But that's the funny thing about idioms, isn't it? In the event, the Verbatim writer was powerless to stay the course of linguistic change, as English speakers chose not to stay the course with the original meaning of "stay the course."