They were all equipped with passive sonar (which just listens). Some had active sonar (the kind that goes "ping").
The early war passive sonar system, Gruppenhorchgerät (GHG), or "group listening apparatus", consisted of two arrays of 24 hydrophones (underwater microphones; the German sonar system usually used piezoelectric transducers), one on each side of the hull near the front, behind the front torpedo tubes. This gave an area to the rear where it couldn't listen, but also allowed it to avoid noise from the U-boat's own propellers. Less useful, the side mounting also stopped it from listening straight ahead. GHG first went into service in 1935.
The next main system used was Kristall-Drehbasisgerät (KDB), "crystal rotating-base apparatus", basically a rotating hydrophone array, which gave all-round listening capability (except to the rear, where the U-boat's own propeller produced too much noise). It was vulnerable to shock, and was easily knocked out by nearby depth charges. The third and final main system was Balkongerät, (Balkon), "balcony apparatus", introduced in 1943, which was a fixed system mounted at the front on the hull, without a deaf spot to the front. This provided the robustness of GHG, with a deaf spot only to the rear, and better performance when at shallow depth. This last point was important, due to widespread use of the Schnorkel.
Passive sonar doesn't give the distance to the target, just the direction. GHG could locate a target to within 1 degree of angle if there was enough signal at high frequencies. If not, its accuracy could be as low as about 8 degrees. Detection range depended on how noisy the target was, how much background noise there was, and sea conditions. Convoys could be heard up to about 100kmn away, and single ships at about 20km. The loudness of the signal could be used to estimate the distance, but not precisely. Over time, the change in angle of the target and an estimate of the speed of the target (e.g., from the propeller speed) could give a better estimate of the distance. Balkon was more sensitive, and could detect ships from further away.
Active sonar systems were installed from mid-1940 on, first seeing use in the Type VIIC. This was Sondergerät für aktive Schallortung (S-Gerät), "Special Apparatus for Active Sound Location". Range was about 3km, similar to British and US active ASDIC/sonar systems. It used a rotating mount, similar to the KDB passive sonar system. It was rarely used: active sonar advertises your position to everybody who is listening. Ships hunting for submarines will hear a submarine's active sonar "ping" while they are far out of its detection range. Active sonar is most useful for submarines when an enemy vessel is known to be nearby, and sonar can be used to locate it well enough to fire a torpedo. However, S-Gerät didn't come with a good computer, and had to operate for a while in order to be able to determine the position and direction of movement of a target, so it was not use-friendly for this purpose. Therefore, it was rarely used. From 1943, a radar detection system (a passive system for detecting when ships or aricraft were using radar to search for the U-boat, taking advantage of active radar advertising its users presence) was installed instead.
An active sonar system with a much better computer, and integrated with the submarine's fire control system, was the Nibelungengerät (AKA Unterwasser-Ortungsgerät Nibelung), used on the Type XXI U-boats. This used Doppler shift of the reflected pulse to measure closing speed, and could obtain a firing solution in as few as three "pings" (for an acoustic torpedo, which didn't need an exact solution, since it would home in on a target).
U-boats were generally equipped with an underwater telegraphy system (UT), which they could use to communicate with each other or with surface ships using Morse code. It saw very little wartime use; this suffered from the same problem as active sonar: it announced your presence.
German surface ships, even heavy warships, were fitted with versions of the GHG system. For example, Prinz Eugen was fitted with 2 sets of 60 hydrophones, detected Prince of Wales and Hood while they were still beyond the horizon.