I’ll note first that I am far from an expert in maritime affairs. I don’t know my capstan from my halyard, although I do know that starboard is right and port is a fortified wine. I am approaching sea shanties as a musical phenomenon, which by this point they are. Hopefully someone can chime in about what exactly all that heaving and hauling was supposed to be accomplishing; I’ll concern myself with the music.
The Shanty Itself
There are really multiple answers to your question, in part depending on how you define “sea shanty” and what you mean by “origins.” This sounds like academic waffle, but it is important to think about what assumptions we have coming into the discussion. "Sea shanty" is often taken to mean essentially any song sung on a ship, for any purpose. If you're asking about the origins of songs being sung on boats, you're out of luck. There is evidence of music being used to ease work on boats going back 4 millennia to Ancient Egypt, and no real reason to doubt that the phenomenon is much, much older still.
But you asked about "sea shanties," which I'll argue are a much more narrowly defined category. They are mainly 19th century in origin, and focused around the sailing culture in the Anglosphere. These are the songs that inevitably get sung and passed around when we talk about sea shanties, not, say, the songs Polynesian voyagers sing, or whatever was going on with those Egyptian boats. These sea shanties are almost entirely English language, often call-and-response in some way, and have common themes that pop up often, like the Napoleonic Wars ("A drop of Nelson's blood wouldn't do us any harm"), women at home ("I want to spend my money on Sally Brown"), and the far-off destinations sailors might be heading to ("And we're bound for the Rio Grande").
The narrowest definition of "sea shanties" confines the term to just the songs that were used to help sailors time their movements while working. This is very evident in some shanties, with the melody hitting a forceful note that was everyone's cue to pull, ensuring that no effort was wasted. "Haul Away, Joe!" is a common example, with the refrain "way, haul away, haul away Joe!" A popular verse in "Haul Away Joe!" refers to King Louis XVI, saying that "King Louis was the king of France before the revolution/until he got his head cut off, it spoiled his constitution!" This obviously places it after the late 18th century, already giving us some clues as to its time period.
This is a separate category from all the songs about sailing, the sea, and the concerns of sailors. Those are separate again from songs that sailors would have sung on board during leisure time, which may or may not have had anything to do with sailing. Nowadays, all three categories are often lumped in as “sea shanties.” This is largely due to the fact that sea shanties are now almost entirely performative, seen as part of a broader folk music tradition. They are divorced from their original context, which changes them and our attitude towards them. That’s a topic for later in the answer, though.
The sea shanty as we know it (well, sort of, we’ll get to that soon) took form in the 19th century. While we do see singing on British ships before then, it is not nearly as widespread or as established as a tradition. Short tasks would have had accompanying chants, and very few of note have made it down to us. The oldest known shanty-like work song from a British ship is referenced in the Complaynt of Scotland in 1549, but sources are pretty scarce besides that.
By the end of the 18th century, though, we’re starting to see some more singing on British ships. The shanty as we know it then pops up around the Americas in the early 19th century. It owes no small debt to the work songs of the African and Afro-Caribbean/African-American people who were transported on many ships as slaves, and who were then put to work making many of the goods that would sustain the shipping economy. In particular, there is a much stronger call-and-response tradition in many African musics, especially work songs, than in European music.
It’s important to note that for African-descended people, singing during work was by no means confined to sea or boat work. Many sources from the time note that no matter the work, no matter the situation, they seemed to be a song that suited the occasion. Some of this may be embellishment; the happy-go-lucky singing slave is a common racist trope (something else we’ll touch on later). Regardless, it’s clear work songs were commonplace among Africans and their descendants in a way that they weren’t among Europeans.
Constant exposure to these work songs helped drive development of the sea shanty, which started becoming popular on ships around the Americas and the Caribbean before proliferating elsewhere. Benefits to coordination and morale were a big reason why they proliferated. But it’s also too “just so” to point to those factors; people do just like singing, and singing a sea shanty was just as fun then as it is now. If anything more so, because, as I’ve noted in another answer here, back before recorded music people had to make their own music a lot more than we do now. Ultimately, not knowing much about British sailing culture of the 19th century, I can’t speculate too much on the exact reason why everyone started singing. Hopefully someone with more maritime experience can chime in.