Your office nemesis, Gary from accounting, went ahead and took all the credit for that big project you worked on together. He sure sold you down the river, didn't he? Yes, truly the worst thing ever associated with that phrase is Gary, with his suspenders and his perfect teeth and his family which actually loves him. The jerk.
But It Came From ...
The river being referred to is either the Mississippi or Ohio, and what are being very literally sold down that river are slaves. Getting sold "down the river" wasn't a pretty fate, even for them. They weren't merely going from one plantation to a different one; chances were they'd be en route to the southern parts of Mississippi, which generally had way worse conditions.
Library of Congress
Which, to be fair, is like choosing between anal warts and gangrene dick, but still.
The earliest known written occurrence of the phrase dates back to 1837 in the Ohio Repository, and the practice of selling slaves down the Mississippi is referenced in Uncle Tom's Cabin. It wasn't until the early 20th Century that people started using "sold down the river" more colloquially. "Hey, you know that awful fucking thing our ancestors did? You know, the thing where they took human beings away from their families and friends and sent them to the shittiest parts of the country to work themselves to death? Let's use that phrase to talk about the time fucking Gary cleaned out the break room fridge and threw away our leftovers without asking. Truly, an atrocity worthy of the term."