At the start of the war the Germans had 29 U-boats in service. The Germans primarily targeted allied warships and merchant ships, until the allies found ways to protect themselves from the u-boats using different methods. These were Q ships, decoy ships that were actually merchant ships armed with heavy guns, so when the Germans fired at them and the merchant ships fired back they were fooled into thinking they were actually warships. Another way to prevent naval loss was to travel in convoy so that all merchant ships could be protected when delivering goods. Mines and depth charges were to explosive ways of defending the ships as the mine would blow up on contact and depth charges would blow up at certain depths. These were both very effective.
The battle of the Atlantic was so crucial that the Germans shot any enemy ship they saw, on one occasion the Lusitania, an American passenger ship where the German saw it and sunk it without warning, the British propaganda portrayed it as criminal although there was some ammunition on board.
Towards the end of the war the Allies had so many ships the Germans could not possibly sink enough of them to make an effect on the battle for the Atlantic, they did not have the firepower and the resources that the Allies had so the campaign was called off.