Valencia, having drawn 1-1 at home, needed just 17 minutes to cancel out Schalke’s away goal when Ricardo Costa headed them in front before but the home side turned things around.
Farfan drew Schalke level with a brilliant 25-yard free-kick in the 40th minute before Mario Gavranovic netted seven minutes into the second half. Schalke then sealed the victory and qualification when Farfan netted their third deep into injury-time.
Schalke came into the game in disappointing form and with question marks over the long-term future of coach Felix Magath,.
In the eighth minute Valencia defender Jeremy Mathieu saw a shot deflected just wide, and nine minutes later the visitors hit the front.
Mehmet Topal fired in a fierce cross that Costa did well to head past Manuel Neuer at his near post.
Schalke’s Spanish striker Raul had a chance to cancel that goal out almost immediately but he headed wide, and soon after that Valencia almost made it 2-0 but Christoph Metzelder did well to block Aritz Aduriz’s effort.
The Royal Blues grabbed their chance to draw level just before the break when they won a free-kick just outside the area.
It was a dangerous position, and Farfan made the most of it as he curled a free-kick into the top corner past Vicente Guaita, who got a fingertip to the ball but could not keep it out.
It got better for Schalke at the start of the second half. Guaita could only push a cross down to Farfan inside the area, and although the Valencia goalkeeper managed to save the Schalke goalscorer’s resulting shot, the ball fell perfectly for Gavranovic, whose effort hit the inside of the near post, rolled across the line and hit the far post as well before going in.
That goal set Schalke on their way to the last eight, but they had to survive some nervy moments before the end.
Valencia could not force an equaliser though, and Schalke twice came close to killing the game off late on when Gavranovic hit the woodwork on separate occasions.
But Schalke sealed it as Farfan broke away before dinking the ball over Guaita to settle the contest.
Neuer, Uchida, Howedes, Metzelder, Escudero, Farfan, Kluge (Sarpei 81), Matip (Papadopoulos 60), Jurado (Draxler 76), Raul, Gavranovic.
Guaita, Bruno, Navarro, Ricardo Costa, Mathieu, Banega (Costa 70), Topal, Joaquin, Pablo (Soldado 64), Mata, Aduriz (Oliveira 75).
Referee: Jonas Eriksson (Sweden).