Earlier in the race Vettel, who was on pole, led only thanks to Ferrari team-mate Kimi Raikkonen being told to move aside.
However, the German crashed out in front of his home fans on lap 52 with rain causing most of the drivers problems.
Hamilton, the reigning world champion, took the chequered flag for a record-equalling fourth German Grand Prix win, ahead of his Finnish team mate Valtteri Bottas and Ferrari's Kimi Raikkonen.
German Grand Prix
Grosjean: German GP key to saving seat
03/10/2018 AT 15:13
"I've never had a race like this," he said.
"After racing for so many years you never know when you're going to have a better race than your best race, but this is right there with them."
It was the 66th win of his F1 career and minutes after the finish, the heavens opened with an array of thunder and lightning and a fierce downpour that would have stopped any race.

Lewis Hamilton (Mercedes) - GP of Germany 2018

Image credit: Getty Images

"Miracles do happen, mate," Hamilton's race engineer Pete Bonnington told him over the radio after a slow-burner that turned into one of the great comebacks of the Briton's already stellar career.
Hamilton, who threw himself across the pitlane wall into the arms of his mechanics and also got a bear-hug from delighted Mercedes-Benz boss Dieter Zetsche, seemed about as stunned as anyone.
"The rain has come down and washed away all the negativity and its a glorious day," Hamilton said, his voice wobbling. "It couldn't be a better day."
Beaten by Vettel in his home British Grand Prix at Silverstone two weeks ago, where he went from pole to the back of the field and up to second, he had turned the tables completely.
Vettel's plight follows a terrible run for drivers at their home circuit this season with Hamilton suffering heartbreak at Silverstone, Esteban Ocon and Pierre Gasly crashing out in the first lap in France and Charles Leclerc and Lance Stroll receiving DNFs in Monaco and Montreal respectively.

Sebastian Vettel - F1 2018 Hockenheim

Image credit: Getty Images

Vettel now has 171 points to Hamilton's 188.
He pummelled the steering wheel angrily after the car crumpled against the tyre wall, the lap 52 incident bringing out the safety car.
"I had it in my hands -- small mistake, big disappointment," said the German.
"It was just one of those moments. It was my mistake so apologies to the team. They did everything right."
Daniel Ricciardo's bad weekend continued has he was forced to retire, but there was fantastic news for Nico Hulkenberg in front of his own fans as he secured a fifth-placed finish.
Max Verstappen was fourth for Red Bull whilst Romain Grosjean took sixth for Haas ahead of Mexican Sergio Perez and Frenchman Esteban Ocon for Force India in seventh and eighth.
Sweden's Marcus Ericsson put Sauber in the points with ninth and New Zealander Brendon Hatley was 10th for Toro Rosso.
German Grand Prix
F1 chiefs swayed Mercedes to help German GP
09/09/2018 AT 12:01
German Grand Prix
German GP back on 2019 F1 calendar after new deal agreed
31/08/2018 AT 13:44