Germany finished top of the section and will play England on Sunday in Bloemfontein. Ghana, the only African team to reach the last 16 so far, came second and will face the United States on Saturday in Rustenburg.
Australia beat Serbia 2-1 in the other match in the group, a result which meant both teams were eliminated.
Ghana now carries the hopes of Africa in the first World Cup to be hosted on the continent with Ivory Coast needing a miracle to qualify from Group G because of an inferior goal difference.
"I hope the majority of South Africans will continue to support us... I hope we will benefit from massive support here in South Africa," said Ghana coach Milovan Rajevac.
"We've created wonderful chances to score but we had a bit of bad luck and we failed to hit home. Perhaps we lacked a bit of concentration... I hope we can score goals going forward.
"We have our next match with the US so we have to forget the game against Germany and move on," he added.
The Germans struggled to break down a solid Ghana defense before Ozil struck the winner in a match that saw two brothers — Ghana's Kevin-Prince Boateng and Germany's Jerome Boateng — face each other for the first time at a World Cup.
With Cacau replacing the suspended Miroslav Klose as their lone striker, Germany carved out chances with the Brazilian-born forward testing goalkeeper Richard Kingson with a low drive after only three minutes.
Cacau then set up Ozil with a perfect through ball in the box but the playmaker, with only Kingson to beat, could only manage a weak shot.
Ghana's defense, led by captain John Mensah returning from injury, gradually shut down the supply lines to Cacau and Lukas Podolski, forcing the Germans to try their luck with long-range efforts.
Germany poured forward, desperate for a goal, and had keeper Manuel Neuer to thank for not conceding a goal on the break when he pulled off a point-blank save from a Kwadwo Asamoah shot early in the second half.
Ozil finally broke the deadlock when he thundered a left-footed shot from 20 meters past Kingson.
Richard Kingson; Hans Sarpei, Jonathan Mensah, John Pantsil, Jonathan Mensah, Anthony Annan, Kevin-Prince Boateng, Andre Ayew (Dominic Adiyiah, 90), Kwadwo Asamoah, Prince Tagoe (Sulley Muntari, 64), Asamoah Gyan (Matthew Amoah, 81).
Manuel Neuer; Philipp Lahm, Per Mertesacker, Arne Friedrich, Jerome Boateng (Marcell Jansen, 72), Bastian Schweinsteiger (Toni Kroos, 81), Sami Khedira, Thomas Mueller (Piotr Trochowski, 67), Mesut Oezil, Lukas Podolski, Cacau.