Not sure of the exact chapter, but Yamato was captured when Yamato, Aoba, and Motoi go outside to investigate during Naruto's training. They come across Kabuto as he is captured by Kurotsuchi. Yamato creates a wooden beam that Aoba can walk on to approach Kabuto and read his mind like he did with Kisame, but Kabuto sheds his body to free himself. He goes after Yamato and, before being captured, Yamato pushes Motoi out of the way.
Kabuto escapes to the Mountains' Graveyard where he and Tobi gather information from Yamato about the Allied Shinobi Forces. Kabuto also studies Hashirama's DNA within Yamato to strengthen the White Zetsu Army. Rather than kill Yamato when he has fulfiled his usefulness, they suspend him opposite Hashirama's clone for future use.
There are no given details of when Yamato changed his side, so I would assume he was forced to serve Kabuto/Madara/Obito/Zetsu while he was captured.