Alvaro Morata broke the deadlock in the 17th minute with a towering back-post header from a corner – but then saw another close-range effort ruled out for a debatable offside call.
Manchester City winger Torres made it 2-0 after 33 minutes when he volleyed the ball in after Dani Olmo’s header hit the crossbar.
Germany, who had only needed a draw to win the group, fell further behind just five minutes later when City midfielder Rodri nodded in from a corner.
Spain captain Sergio Ramos – who saw two penalties against Switzerland saved – was forced off just before half-time with what appeared to be a hamstring problem.
It was 4-0 after 54 minutes when Spain left-back Jose Gaya made the most of some uncharacteristic wayward German defending to set up another for Torres.
The City winger completed his hat-trick with 18 minutes left when he curled a first-time shot past Manuel Neuer.
Substitute Mikel Oyarzabal knocked in a late sixth to complete a memorable victory for La Roja.
Relegation in the group is still to be decided after Switzerland’s clash with Ukraine in Lucerne has was called off following more positive Covid-19 tests in the visitors’ camp.
France had already made sure of their place in the last-four tournament, and finished off their A3 campaign with a 4-2 win over Sweden, who are relegated to League B.
Sweden had taken an early lead at Stade de France through a deflected shot from Viktor Claesson after just four minutes.
Chelsea forward Olivier Giroud soon equalised, before right-back Benjamin Pavard made it 2-1 in the 36th minute after Marcus Thuram’s shot was blocked.
Substitute Kylian Mbappe teed up Giroud to head home his second just before the hour.
Robin Quaison struck a late consolation for the Swedes, but Kingsley Coman had only been on for a few minutes when he scored a fourth for Les Bleus in stoppage time.
Portugal, the inaugural Nations League winners, scored a late winner from Manchester City defender Ruben Dias to sign off with a 3-2 win over Croatia, who finished with 10 men in Split.
Mateo Kovacic put the home side ahead when he tucked in a rebound after 29 minutes, but Croatia – who themselves could have been relegated if results went against them – had Marko Rog sent off for a second caution soon after the restart.
Manchester City defender Dias equalised in the 52nd minute with his first international goal, with Joao Felix adding a second for Portugal on the hour.
Kovacic hauled the World Cup runners-up level five minutes later, but Dias sealed victory for Portugal when he capitalised on a fumble from the Croatia goalkeeper.
Elsewhere on Tuesday, Montenegro secured promotion as winners of group C1 after a 4-0 victory over Cyprus, who are relegated, while Luxembourg and Azerbaijan played out a goalless draw.
Gibraltar are promoted from D2 after a 1-1 draw at home against Liechtenstein, while the Faroe Islands are also set to move up to League C after a 1-1 draw away at Malta put them top in D1.
Latvia won 5-0 at Andorra, who finished with 10 men and sit bottom of D1.