The $ sign is older than the United States dollar, and nobody can show where it came from. The best-supported answer is that it was a scribble for pesos.
The currency came first. Etymonline says the word dollar goes back to the Joachimstaler, a large silver coin first minted in 1519 from ore mined at Sankt Joachimsthal in Bohemia. Low German shortened it to daler, and English had dollar by 1600.
From the 1580s, English colonists in America used the word for the Spanish peso, the "piece of eight," a silver coin of similar size. Trade with the Spanish Indies made it the most familiar coin in the colonies. Congress made the dollar the federal unit of account in 1786, and the first U.S. silver dollars were struck in 1794. Britannica says the earliest printed dollar sign appears in 1797, and Wikipedia notes that the first dollar coins carried no symbol.
So the sign was borrowed from pesos. The leading theory is that traders abbreviated pesos as "ps," and over time the s was written on top of the p until only the p's vertical stroke was left. The best early example is a 1778 document by Oliver Pollock, an Irish-born trader who helped fund the American Revolution. Britannica says the P of his "Ps" seems to overlap the s, then adds a caution: there is little evidence the symbol was in general use then, or that Pollock's version spread. Wikipedia says business letters from the West Indies in the 1770s use the sign for the peso.
Other theories compete. One links the S and bar to the ribbon-wrapped Pillars of Hercules on Spanish coat of arms and on coins minted at Potosi. Another reads a Potosi monogram, P T S I, as the source. Britannica finds little evidence for the pillars and notes that no document shows the numeral 8 used for the Spanish dollar.
One story is plainly wrong. It says the sign is a U laid over an S, for "United States." Ayn Rand repeated it in Atlas Shrugged in 1957. Britannica says it has no factual basis.
As Arthur Nussbaum wrote in his 1957 A History of the Dollar, the dollar sign "is connected with the peso, contrary to popular belief."