The battlefields were typically larger than individual cities, and often involved lines of subsequent defenses. So for instance in the defense of Italy in 1944, the Germans employed a series of defensive lines across the peninsula (eg the Gustav Line, the "Hitler" line, the "Caesar" line). All of these made use of high ground and key positions of fortification. High ground gave spotters a chance to see the fall of artillery rounds, giving a distinct advantage, as well as all the other benefits.
Along these larger defensive fronts were individual strong points. One famous example is Cassino town (and mountain), which was the lynch pin of Germany's Gustav Line in southern Italy. The town and the slopes all around (but not the monastery itself, for futile cultural preservation reasons) were occupied by defensive positions, usually entailing trenchworks or fortified foxholes, machine gun nests, minefields, and generous use of barbed wire. Mines included anti-personnel versions like the German "Bouncing Betty," and also larger anti-vehicle mines (and sea mines, when applicable). Snipers were extremely common and horrifically effective at picking off enemy officers. Allied officers quickly learned to conceal their rank insignia or risk getting killed by ever-present snipers. Artillery too played a major role in the defense of fortified positions in the second world war, and both sides got better and better at it as time went on, unfortunately. Fields of fire would be cleared for artillery by felling trees (to enable line of sight), and artillery was often "zeroed in" on key targets (like crossroads) ahead of time using forward spotters, so that during an attack the zeroed in guns could fire on known targets with assurance. The Germans also liked to hide panzers or tiger tanks within structures, with the field of fire for their large guns pre-cleared. They also liked to occupy strong buildings. At Cassino, the Germans controlled all the biggest structures in the town, like the Hotel des Roses. When they had the time and opportunity, the Germans also used steel bunker emplacements (I forget the German name right now).
In addition to field artillery, some places had larger, immobile guns. The French towns of North Africa had coastal batteries with gigantic guns inside moldering old forts from the 19th and 18th century which caused terrible damage to the bumbling Allies during the early parts of the war. And, especially further in to the war, the skies were defended as well as possible by anti-aircraft guns. German cities practically bristled with AA guns in 1944 and 1945 as they attempted to slow down the constant, day and night bombing by Allied Air Forces. The Germans also employed massive guns mounted on rail cars (for instance the two that terrorized the beachhead at Anzio in Italy in 1944 and never then hid from Allied aircraft inside a ruined rail tunnel between volleys).
Also in the air in defense of cities were airplanes, of course. Most famous is Britain's defence of the country by the RAF during the Blitz, aided by a handy new invention called RADAR. German fighters also exacted a heavy toll of Allied bomber crews over European cities.
On a larger scale, the Germans in particular often operated on the doctrine that the best defense was a good offense. They had a vicious propensity to counterattack with their highly mobile panzer, grenadier, and airborne units, especially into the extended flanks of Allied attacks. This is one lesson the Allies were quite slow to learn, and we see the Germans employing counterattack effectively right up at the waning of the war, at the Bulge.