What made Sakuya decide to switch sides?
I don't remember what exactly happen in anime, so I will talk about Sakuya in VN.


   Sakuya's loyalty lies completely with Chihaya. And Chihaya is not that loyal to Gaia. In her VN she gives up on Gaia few times. And Sakuya always follows.
 And while Sakuya does insult Kotarou, it is mostly playful messing around. He never really endangers him. He is basically trolling him, because he knows he gets reaction out of him.


Why Sakuya doesn't like Kotarou is explained in the VN:


   Sakuya is same as Kotarou, having the Rewrite ability. And like Kotarou, he abused it to the point he lost his humanity and became a familiar. And he simply sees himself in him and doesn't like to be reminded of his past. But when situation gets serious enough, he puts all joking aside and begins treating Kotarou properly.


tl;dr; Read the VN. The anime is terrible, TERRIBLE, adaptation.