Anis Ben-Hatira put the home side in front in the 14th minute, adding a second 12 minutes from time.
Marvin Compper pulled one back for Hoffenheim, who lost Ryan Babel to an early red card, before Raffael sealed the victory with the last kick of the game, sparking celebrations inside the Olympiastadion as Berlin moved above Cologne into 16th place.
They will now play the side who finish third in the 2. Bundesliga, which will be either Fortuna Dusseldorf, Paderborn or St. Pauli, whose fate will be decided on Sunday.
Ben-Hatira gave Berlin a dream start with a direct free-kick which flew past everybody on its way into the far corner of the goal.
Babel was then shown a controversial red card when he was pushed into a Berlin player and got involved in a small fracas, receiving first a yellow card and then the red, even though he had not previously been warned.
Pierre Lasogga missed the chance to double Berlin's lead just before the interval, but with news of Cologne's collapse at home to Bayern filtering through, Berlin knew that they could even draw and move up a place.
Hoffenheim tested their nerves in the last 20 minutes, but Hertha stood strong and even doubled their lead through Ben-Hatira in the 78th minute.
Compper brought Hoffenheim back into the game with a goal from close range in the 85th minute and goalkeeper Tom Starke almost scored a dramatic equaliser as he went forward for a corner in the last minute.
However, the real drama followed immediately with Raffael running the full length of the pitch to deposit the ball into the open goal and seal Hertha's win, leaving them two more games to save their top-flight status.
Hoffenheim coach Markus Babbel, who was in charge of Hertha until Christmas, was the first to congratulate Raffael as the final whistle blew.