Roger Federer kept alive his chances of winning a record eighth Wimbledon crown when he rallied to beat Marin Cilic in five sets.