Didier Drogba had fired Chelsea ahead with a low drive three minutes into the second half but substitute Eren Derdiyok levelled with a far-post header in the 73rd minute.
That was still looking a good result for Chelsea but they lost their concentration from a stoppage-time corner when Friedrich connected with a solid header.
Germany's Leverkusen, who face Genk in their last group match in December, top the group on nine points, one ahead of Chelsea and Valencia and are guaranteed progress because of their record against both Chelsea and the Spaniards, who thrashed Genk 7-0 on Wednesday.
A win for Chelsea against Valencia at Stamford Bridge or a goalless draw would send them through while a score draw or defeat would see the Spaniards into the last 16.
"It is always a bit lucky if you score in the last minute but we believed in it, we have a young team," Leverkusen's Ballack said after victory over his former club.
"During the game I didn't feel we believed that we could win the game. Chelsea felt that and got stronger and scored and deserved it , but then we had nothing to lose, we risked a bit more and I think we deserved the victory."
After seeing his expensive side fall to a fourth defeat in seven games in all competitions, Chelsea coach Andre Villas-Boas said: "We had a big chance to go 2-0 up but our focus must be on Valencia now. It will be a good head to head battle.
"We were compact and disciplined until the last moment. In this competition if you make a mistake you pay a heavy price."
After a mediocre first half where Ballack, marking his 100th international appearance with what turned into a command performance, hit the crossbar with a header, Chelsea took a deserved lead early in the second half when Drogba turned well in the box to score with a low shot.
However the hosts, beaten finalists 10 seasons ago, forced their way back into the game and levelled with a well-crafted goal for substitute Derdiyok a minute after coming on.
They then sent their fans wild when Chelsea fell asleep at a corner and Friedrich rose high to strike at the death.
"We've been suffering a lot, conceding a lot of goals, but we haven't conceded from a corner for a long time - I think that was the first time," said Villas-Boas.
"We were compact and disciplined in set plays but one detail in the last moment... we need to get the concentration and focus right. No excuses - if we have to go one more step up that's what we have to do."
Delighted Leverkusen coach Robin Dutt said his players deserved the win after delivering a passionate performance.
"Just like against Valencia we turned around a game," he told reporters. "That shows character and the quality of our team. We will try to hold on to top spot in Genk."
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.