Lester, who can become a free agent at the end of the season, had been the subject of trade rumours leading up to Thursday's 4 p.m. ET deadline to deal players without having to pass them through waivers.
With Boston fading from post-season contention, he was pulled from his scheduled start Wednesday night against Toronto.
Lester, 30, was Boston's only All-Star this year. He's 10-7 with a 2.52 ERA.
He joins a stellar Oakland rotation that includes All-Stars Scott Kazmir and Jeff Samardzija, who was acquired from the Cubs earlier this year, and ascending youngster Sonny Gray.
The A's gave up a fine power hitter in Cespedes, a 28-year-old Cuban who's in his third big-league season. He hit 49 homers over his first two campaigns and has another 17 this year.
Gomes was a useful part on Boston's World Series-winning team a year ago but is just a .244 career hitter.