While the Straits of Gibraltar are narrow, there are other factors that allow U-boats (and other submarines) to slip through it.
Firstly, while the Strait is narrow, there are geographical conditions that make it hard to defend against submarines. It is deep. At its deepest parts, it has a depth of over 1,200 feet. During WWI, the British were able to successfully establish a barrage of nets and mines across the Dover Strait, and less successfully, across the Strait of Otranto and the northern exits to the North Sea. The Dover and Otranto Barrage largely consisted of nets towed by requisitioned fishing ships (drifters). These nets could trap U-boats passing through, or indicate their presence to patrol boats to attack. However, the nets were only effective against U-boats at relatively shallow depths. The Dover Strait, which was successfully closed to U-boats by the barrage, has a maximum depth of only 180 feet, pushing U-boats into the arms of the nets. Mines have similar issues with depth. They need to be moored to the sea floor, or they will float into shipping lanes and pose a danger to friendly traffic. This makes it impractical to lay mines in deep water; they can't be given a long enough mooring cable. The North Sea Barrage was a vast minelaying campaign to block the North Sea exits; this was only feasible because the average depth of the areas where the mines were laid was less than 300 feet. The Otranto Strait had water of similar depth to the Gibraltar Strait, meaning that mines couldn't be laid there. This meant that it was much less effective than the Dover Barrage.
Another problem with Gibraltar is the current conditions. The Strait sees strong flows of water in and out of the Mediterranean, with warm surface water flowing eastwards on the surface, and colder water flowing westwards deeper down. This gives a submarine several advantages. The strong currents make minelaying much harder, as the current will cause the mines to drag their anchors or snap the mooring cable. The strong eastward current can help to sweep subs quickly into the Mediterranean; meanwhile, it makes it harder for patrol craft to keep station in the narrowest part of the Strait. The discontinuity between the warm surface current and the cold subsurface current creates difficult conditions for sonar. Sound waves will reflect off the discontinuity between the two layers; a submarine that is below the discontinuity is hard to detect with active sonar. The varying water temperatures and flows also confuse passive sonar, with the sound waves being refracted and shifted differently and in surprising ways.
Finally, there are other ways into the Mediterranean, other than the bits of Gibraltar that Britain can block. The southern edge of the straits was held by Spain, neutral in both World Wars. The British could not legally mine or patrol Spanish waters, while German subs could legally pass through them. This was especially true in WWII, where Spain was aligned with Germany. The North Sea Mine Barrage was only achievable with Norwegian support, grudgingly obtained through diplomatic pressure (and the British blockade); Spain was less willing to offer support than Norway, and Britain had less leverage. In addition to this, U-boats could enter the Mediterranean through other routes. In WWI, a number of U-boats were moved to the Mediterranean overland. They were split into sections, which were moved to ports in Austria-Hungary and the Ottoman Empire by rail. At these ports, the subs were reconstructed and launched for operations. In WWII, a flotilla of coastal U-boats operated in the Black Sea. These had travelled up the Elbe, then transferred overland to the Danube, which took them to the Black Sea. Given that U-boats could still enter the Mediterranean by these routes, there was little point in expending vast amounts of resources blocking the Strait if it would be relatively ineffective.