By using the “Convoy” approach.
A convoy is basically a group of ships that sails together; the convoy is surrounded by a screen of escort ships that, like sheepdogs, protect and fend off the attacks from U-boats (the wolves). Consider that at that time (and always I think) you cannot protect each ship sailing alone, there aren’t enough escort armed ships to do that.
By forming a “flock” of ships you can use few fast and well-armed patrol ship to keep away the U-boats that are also forced to operate underwater where their speed is reduced causing them, most of the time, to lose contact with the convoy; in addition, during this work of interdiction you can sink few U-boats as well!
Also, the intelligence from the decryption of German messages helped a lot allowing the Allies to steer the convoys away from the positions of the U-boats wolf packs.