Roger Federer, a 20-time Grand Slam singles champion, will retire from top-level tennis after the Laver Cup in London this month.