The siege lasted much longer than the numbers might suggest. Count Ernst RĂ¼diger von Starhemberg was defending with only 15,000 or so regular troops, and a few thousand volunteers, against perhaps 170,000 attackers. The Viennese had plenty of time to prepare, several months, in fact. They had put up an outer palisade that they defended vigorously, delaying the Ottomans from the main walls. The Ottomans didn't bring any large siege guns that might have reduced the walls of Vienna quickly, so they resorted to bringing up their trenches close and mining under the walls. Kara Mustafa correctly identified the weakest point in the defenses, an area where two bastions were too far apart, with only a detached revelin between. By the 11th of September, both bastions and the revelin were taken or reduced to uselessness. Kara Mustafa seems to also have wanted to take the city intact, and may have delayed some in hopes of surrender (which meant he would receive the greater share of the wealth of the city). But much of the credit has to go to Starhemberg and the defenders, who made the Ottomans pay dearly (they lost some 20,000 men attacking the city, before the Holy League arrived).
Kara Mustafa seriously underestimated the size of the relief force. The Holy League had come from an unexpected direction, over the rough terrain of the Weinerwald (Vienna Forest). Charles, Duke of Lorraine, and his forces on the League's left reached the Kahlenberg on the 11th, along most of the German troops. They drove off an Ottoman detachment, which certainly reported their presence. Jan III Sobieski's force, delayed by the terrain, didn't reach the ridge summit until after sunset, and probably remained undetected until the next morning. Kara Mustafa sent a force of only 28,000 men and 60 guns, placed opposite Lorraine's Imperial force, and these troops inexplicably failed to improve their defensive positions during the night. Kara Mustafa kept some of his best troops, including many of the Janissaries, in reserve for the final assault on the city.
The Ottomans launched a spoiling attack against Lorraine in the dawn twilight on the 12th. This forced the duke to move troops to protect his artillery, and the two sides continued to pour more men into this battle. This ad-hoc, unplanned battle favored the disciplined German troops. Ottoman command and control was very poor, and while individual units fought hard, there was little or no coordination between elements. On the other hand, the Germans advanced with very good coordination between their artillery and infantry. The Polish force was still slowed by the woods and small detachments of Janissaries. Kara Mustafa's Tatar cavalry, who held the Ottoman left flank, only engaged the Poles once and fled the field. The Holy League, by afternoon, had the Ottomans in a wide crescent, which they slowly pushed tighter. The Ottoman troops were packed in and unable to maneuver as the League pressed them together.
Kara Mustafa might have been able to hold the line until dark, and use the night to reorganize and prepare a better defense. But Sobieski and Lorraine individually came to the same conclusion: They had to gamble and try to break the Ottoman defense before darkness set in. The combined cavalry charge numbered somewhere between 15 and 20,000 horsemen. This shattered the Ottoman line, putting them to flight.
Despite his superior numbers, Kara Mustafa failed to set up a proper defense against the Holy League's approach, and the poor organization of the defense resulted in his defeat.
Wheatcroft, Andrew. The enemy at the gate: Habsburgs, Ottomans and the battle for Europe.