The submarine's main weapon is the torpedo, an underwater missile which advances rapidly in a shallow depth, and when it hits the target ship its powerful warhead explodes underwater, so severely damaging the ship that it often sank rapidly by the flow of water into it, and often went ablaze or even exploded. In night attacks, a single burning ship often lit the convoy's other ships so that they all became clearly visible targets to the attacking submarines.
World War 2 submarine torpedoes usually carried a 300kg warhead, and ran through the water at speeds between 30 knots (the German electric powered type) to 49 knots (Japanese and Italian torpedoes). When fired from a distance of less than a mile, such speeds meant that the torpedo could hit the target before it could make significant evasive maneuvers, allowing the submarine captain to achieve well aimed hits and sink the target with a single torpedo. The only problem was that most torpedo engines released air bubbles which revealed the position of the attacking submarine to the attacked ship's escorts and they hurried to attack the submarine with depth charges (underwater bombs), gunfire, and even by ramming.
This was compensated by being able to quickly launch four or even six torpedoes at different targets and then quickly dive deep and hopefully get away without being detected or at least without being hit. Also, the Germans used an electric-motor type of torpedo which was slower but had no bubbles so the attacked ship escorts could only generally know if the attacking submarine is to the left or right of the ship.
Instead of searching for enemy ships in the big ocean and risk being attacked by their escorts, submarines could instead lay sea mines near enemy harbors and along shipping routes, a "statistical" method which sank many ships and involved less risk for the submarine.
To avoid early detection in day attacks, the submarine is equipped with a periscope, an optical device which lets the captain see the ships on the surface and aim at them while being submerged in shallow depth.
A Schnorkel is similar to a periscope, but it is used to bring fresh air from outside into the shallow submerged submarine to enable it to run its diesel engine, recharge its electric engine's battery, and refresh the air for breathing, without a dangerous surfacing.
When a lonely enemy ship was spotted, especially if it wasn't big, the submarine could surface and use a deck gun to sink it or force its crew to abandon it instead of spending a big torpedo to sink it.
When surfaced, the submarine was constantly in danger of a sudden attack by aircraft. For that it always kept lookouts and was also equipped with anti-aircraft guns. Later during the war electronic receivers were also used to detect the RADAR transmissions of such aircraft before they could be seen or heard, which could be too late.
In addition to the periscope, submarines (mostly American submarines) used a RADAR to detect aircraft and ships around them, at day and night. RADAR was useful not just for defense from threats but also to detect and attack enemy shipping, especially at night or in bad weather. Due to the limited Japanese use of RADAR, the RADAR-equipped American submarines had a great davantage. Late in the war a small range-finder RADAR mounted on the American submarines' periscope mast could even provide accurate target range data for enhanced torpedo targeting.
And finally, submerged submarines could listen to the engines and propellers of nearby ships by using hydrophones, directional underwater microphones which let the operator not just hear the enemy ship but also determine its direction and, with experience, estimate its range. This was particularly useful to determine ships presence around, or overhead, when it was too dangerous to raise a periscope to look, especially when enemy escorts were too close. It was mostly a defensive measure, but in rare cases, target ships were detected and targeted solely by hydrophones, without using a periscope or RADAR because it was too dangerous.
The submarine is a very efficient weapon by allowing its small crew to sink many ships much bigger than itself. But it has to spend many days to find these ships in the ocean because it can't sink them where they're easiest to find - inside their protected harbors (there is a single exception. The daring captain Prien of the German U-47 used an unprotected gap in the perimeter defence of a main British Navy harbor to get inside, sink a battleship, and escape unharmed). Furthermore, once inside a harbor, a huge battleship can be as easy target as any merchant ship.
The Italian Navy was the first to understand this difficult opportunity and develop a series of solutions to the problem of penetrating a heavily guarded enemy harbor and sink ships in it. The 10th light flotilla of the Italian Navy was an elite unit of innovative and daring volunteers which developed and used those special warfare methods. These methods allowed them to achieve amazing efficiency ratios in which a handful of daring commandos could sink several large ships, often without casualties to themselves, an amazing achievement which was repeated again and again despite heavy enemy protection. Other navies copied the Italians in World War 2, but with much fewer successes.
The Italians first developed the military underwater breathing device, which unlike modern civilian diving gear, did not emit any bubbles to the surface which could reveal the presence and location of the divers. After being brought near the enemy harbor by a submarine, or by secret agents to a nearby beach, the divers, nicknamed frogmen, could swim into the harbor, equipped to penetrate the underwater defense nets. When they arrived at their target ships, they could attach to their underwater hulls, in the optimal position in terms of damage, small underwater mines which could be attached either mechanically or magnetically, and were set to explode later by a time delay mechanism or even by a simple device which measured the ship's travel distance and ensured it would explode out of the harbor in deep water, long after the divers escaped.
Later the Italians improved by creating the human torpedo, a torpedo-like underwater vehicle with two seats for the divers to ride on, steering handles, and a big detachable warhead. This gave them greater range, speed, and firepower than they had by swimming with small mines. Now their two-men units could sink or at least severely damage the biggest battleships, and so they did. They also especially modified some of their attack submarines to carry those "human torpedoes".
Other developments included midget submarines, which are slow and short ranged tiny submarines (used mostly by the Japanese and British), or even torpedo-like submersibles which sailed just under the water surface and could not dive deep, with the operator sitting inside a small cockpit, like a fighter pilot, and carrying one or two torpedoes attached from outside (used mostly by the Germans late in the war), and Explosive speedboats with an eject seat, which made a silent surface approach followed by a final charge at full speed, with the operator-swimmer leaving the speedboat with an eject seat after aiming it to its target (operated by the Italians). None of these was a suicide weapon. The Japanese developed and used suicide versions of these weapons, and the Germans developed a remote controlled explosive speedboat. The success rate of all these weapons was significantly lower than that of the original Italian human torpedo, which allowed the divers to silently reach the target ships, precisely attach large time delay warheads, and escape before a series of explosions revealed their visit to the surprised defenders.
Back to part 1 : Submarine warfare in World War 2, German U-boats and in other navies