The match seemed destined to finish goalless with both sides reluctant to commit bodies forward, but Bayern Munich's Gomez settled the match in Germany's favour when he rose to head Sami Khedira's deflected cross beyond Portugal goalkeeper Rui Patricio on 72 minutes.
Portugal enjoyed their best moments when they were forced to throw caution to the wind in the death throes of the match, but their defensive approach did not contribute to an appetising spectacle as men such as Cristiano Ronaldo, Nani and Helder Postiga were forced to live off scraps for most of the evening.
Germany will reach the last eight if they beat the Netherlands on Wednesday night. It should be a fascinating occasion with the Dutch needing to avoid defeat having lost 1-0 to Denmark in Kharkiv earlier in the day.
Portugal showed enough against Germany in the final 18 minutes to suggest they can beat Denmark on Wednesday, but they would be better served to adopt a more positive outlook against Morten Olsen's side.
The first period was a non-event in terms of being an event as Lukas Podolski drove a shot into the arms of Patricio on eight minutes with Thomas Mueller screwing an effort wide seven minutes before half-time.
The best chance of the half fell to Portugal a minute before half-time as Pepe saw a shot come back off the underside of the bar before bouncing to safety.
Portugal claimed for a goal, but replays showed it had not crossed the line.
Jerome Boateng was forced to make a fine tackle on Ronaldo as he prepared to shoot inside the Germany box on 64 minutes. Gomez struck eight minutes later to settle the game moments before he was going to be replaced.
Watched by Real Madrid coach Jose Mourinho, Portugal could have rescued a draw in the closing moments as Nani saw a cross clip the top of Germany's bar while Ronaldo's effort from distance was beaten out by Germany goalkeeper Manuel Neuer as it dipped and swerved in the air.
Neuer then spread himself superbly well to save from Varela after his fellow substitute Nelson Oliveira had shown some intelligence to play provider.
Portugal have never been eliminated from the group stage of the European Championships in five appearances. They at least know what is on the line before they confront Denmark.