Anis Ben-Hatira gave Berlin the lead in the 13th minute and Ramos doubled the advantage from the penalty spot after 53 minutes.
Sejad Salihovic then levelled things up with a penalty and a free kick with nine minutes to go but Ramos provided the winner just three minutes later.
Berlin snapped a two-match losing streak to move on to 18 points while Hoffenheim have lost three of their last four contests and sit five points further back.
The visitors had two chances in the first four minutes as Sami Allagui's shot went just wide in the third minute and Ben-Hatira was off target seconds later.
Ben-Hatira proved more accurate in the 13th minute, striking home a cross from Ramos nine yards out.
Salihovic was close twice from free kicks in the 26th and 30th minutes but Berlin keeper Thomas Kraft was well placed.
Kraft made another fine stop on Salihovic in the 49th minute to keep his team ahead.
But it was 2-0 four minutes later as Ramos was fouled in the box by Jannik Vestergaard. The Colombian stepped and converted from the spot.
Hoffenheim got a goal back after 70 minutes as Sahilovic struck his spot kick into the bottom corner after Sven Schipplock was taken down in the box by Sebastian Langkamp.
And the Bosnian equalised with eight minutes left as he perfectly placed a free-kick from just outside the area.
Ramos didn't let the hosts dream too long, though, as he netted his second to make it 3-2 in the 84th minute, heading in a cross from Peter Pekarik.