Put yourself in a car with all the windows blacked out. You've got your speedometer, a map, a compass and a stopwatch. Add trigonometry. Even today, that's how it's done in case the Inertial Navigation System fails. Pretty amazing.
Well, you're forgetting a couple of factors.
Firstly, all boats prior to the type XXI were not true submarines but rather fast surface ships that could spend a very limited amount of time submerged, running on batteries and using stored oxygen and carbon dioxide scrubbers. When not under attack, in ambush or actively avoiding detection subs would be on (or near in late WWII) the surface, which was the vast majority of the time.
Secondly, the sea is vast and deep, so generally the risk of colliding or running aground while unawares is small. Navigation would be done much as it always has: by tracking speed and heading, and verifying via celestial observation.
Sonar - basically underwater echo-location using sound waves. Patented in 1913 by a German physicist, Alexander Behm, after the Titanic disaster, it was developed by the U.S. Coast Guard in 1914. It was deployed on British submarines in 1915, and found widespread use in submarines and sub-hunters later that year as WWI unfolded.