Before it was a weapon, a torpedo was a fish. The name described what it did to people who touched it.
The electric ray carries muscle cells in its fins that can deliver a discharge. The University of Texas Marine Science Institute puts the jolt at up to 220 volts. Latin named the fish torpedo, a word that first meant "numbness" or "sluggishness," from torpere, "to be numb." Samuel Johnson's dictionary described a fish that "benumbs the hand that so touches it."
English borrowed the fish name in the 1520s, according to Etymonline. Another English name for it was cramp-fish.
The jump to warfare came in the age of the first submarines. Wikipedia says that in 1775 David Bushnell reportedly named his submarine "American Turtle or Torpedo." Etymonline dates the sense of an explosive for destroying enemy ships to 1776, though at first it meant something like a floating mine.
Robert Fulton is the inventor most tied to the name. In 1800, Napoleon commissioned him to design a submarine, and Fulton built the Nautilus in France. He used "torpedo" for the explosive charges his submarine would deliver. The Marine Science Institute suggests he may have been thinking of the ray's jolt. After moving to Britain in 1804, Fulton developed more of these weapons for the Royal Navy. Wikipedia describes them as what we would now call mines.
For most of the 19th century, a torpedo could be almost any underwater explosive. When Admiral Farragut supposedly shouted "Damn the torpedoes!" at Mobile Bay in 1864, he meant a minefield. Wikipedia calls the famous line itself apocryphal.
The modern meaning arrived with Robert Whitehead, who in 1866 invented the first effective self-propelled torpedo. From about 1900, both Wikipedia and Etymonline say, the word has meant only that kind of weapon.
The weapon sense kept generating words. Etymonline records the torpedo-boat by 1810 and the verb torpedo, to sink by torpedo, from 1874. A figurative sense appears by 1895.