The holders secured a 4-2 win after extra-time at Santiago Bernabeu to seal a 6-3 aggregate triumph and a place in the semi-finals.
Bayern were on the wrong end of three controversial refereeing decisions, with Arturo Vidal given a second yellow card for what appeared to be a fair challenge, before Cristiano Ronaldo completed his hat-trick with two goals from offside positions in extra-time.
Lahm, who will retire at the end of the season, praised his team-mates for their fighting spirit, but conceded that it was a difficult way to play his last game in the competition.
"Despite a strong performance, it was not enough to continue to the semi-finals," he wrote on Instagram. "It is a bitter farewell from the Champions League — especially for me personally, after so many years.
"However, the attitude of the team and their strength to fight against problems, shows big potential — @FCBayern will be back!"
Trotz eines starken Auftritts hat es nicht für das Weiterkommen gereicht. Es ist natürlich ein bitterer Abschied aus der Champions League - auch für mich persönlich nach so vielen Jahren. Der Charakter der Mannschaft, sich gegen alle Unwägbarkeiten zu stellen, zeigt aber das große Potential - der @FCBayern wird wiederkommen! #MiaSanMia Despite a strong performance, it was not enough to continue to the semi finals. It is a bitter farewell from the Champions League - especially for me personally, after so many years. However, the attitude of the team and their strength to fight against problems, shows big potential - @FCBayern will be back! #MiaSanMia
Bayern chairman Karl-Heinz Rummenigge accused the officials of "killing" the Germans' hopes during a hugely entertaining match.
"It was an extremely emotional spectacle. You can only compliment both teams. It was great football," he said.
"Unfortunately it was determined by the referee, at our expense.
"It wasn't even a foul by Vidal, but of course Vidal was already at risk. This weakened us, of course, as it did in the first leg when [Javi] Martinez was sent off.
"It killed us. That must be said very clearly. Arturo gets to the ball in front of the Madrid player. It was not a foul. That's why it shouldn't be a yellow-red card.
"If you play against a team like Real with 10, it becomes difficult. The whole team fought well. Then we were in extra-time and suddenly it's 4-2 thanks to the most curious decisions from the officials."