At some point, because of old territorial claims based on basins, drainages, or rivers (or something), parts of modern day northern Minnesota and North Dakota were Canadian, and parts of modern day southern Alberta and Saskatchewan were held by the United States.
At some time, these lands were traded between the two countries, forming the Western border between them along that one line of latitude.
I can't find anything online that says when that happened-- so, when did that happen?
The question here has more to do with ambiguity regarding the boundaries, rather than an actual transfer of land. The original boundary between the United States and British North America (remember "Canada" at this time only referred to the St Lawrence Valley and Upper Great Lakes) was set in 1783 by the Treaty of Paris. The boundary was an imaginary line that cut through the centre of four Great Lakes, extending to Lake of the Woods and then continuing due west to the imagined headwaters of the Mississippi River. The territory to the west of the Mississippi belonged to Spain, while the Hudson's Bay Company had a territorially ambiguous claim to the land north and west of Lake of the Woods.
When the French (who had acquired Louisiana from the Spanish) sold the territory west of the Mississippi to the Americans in 1803, the boundary remained ambiguous - particularly since it was almost exclusively populated by indigenous people, with a scattering of fur traders living among them.
The presence of British traders and soldiers in region around the Great Lakes remained a point of contention between the British and the Americans - one of the provisions of Jay's Treaty in 1794 was the evacuation of their military posts south of the Great Lakes - and was one bone of contention in the War of 1812.
It was not until the war ended that the western boundary between the US and BNA was made clearer: it was established by Article II and Article III of the Treaty of 1818, which stipulated:
It is agreed that a line drawn from the most northwestern point of the Lake of the Woods, along the 49th parallel of north latitude, or, if the said point shall not be in the 49th parallel of north latitude, then that a line drawn from the said point due north or south, as the case may be, until the said line shall intersect the said parallel of north latitude, and from the point of such intersection due west along and with the said parallel, shall be the line of demarcation between the Territories of His Britannic Majesty and those of the United States, and that the said line shall form the southern boundary of the said Territories of His Britannic Majesty, and the northern boundary of the Territories of the United States, from the Lake of the Woods to the Stony [i.e., Rocky] Mountains.
and
It is agreed, that any Country that may be claimed by either Party on the north-west coast of America, westward of the Stony Mountains, shall, together with its Harbours, Bays and Creeks, and the navigation of all rivers within the same, be free and open for the term of 10 years from the date of the signature of the Present Convention, to the Vessels, Citizens and Subjects, of the 2 powers: it being well understood, that this Agreement is not to be construed to the prejudice of any claim which either of the 2 High Contracting Parties may have to any part of the said Country, nor shall it be taken to affect the claims of any other Power or State to any part of the said Country, the only object of the High Contracting Parties, in that respect, being to prevent disputes and differences amongst themselves.
So the 1818 treaty established the 49th parallel as the boundary between the states and provinces that you mention. The boundary in the Pacific Northwest remained contested, however, and was not resolved until the signing of the Oregon Treaty of 1846.