I must admit I'm a little hazy with SW in general, but why did the clones switch sides? I think it was due to some special code being used, but why did they do it? The clones were bred for obedience, but wouldn't switching side be a little questionable?
They were bred for obedience.
And they must obey the one that gave the order, Mr. Palpatine. So you see, they didn't actually switch sides.
In the event of Jedi officers acting against the interests of the Republic, and after receiving specific orders verified as coming directly from the Supreme Commander (Chancellor), GAR [Grand Army of the Republic] commanders will remove those officers by lethal force, and command of the GAR will revert to the Supreme Commander (Chancellor) until a new command structure is established.
It was his plan all along, make a fake war between the droid army and the clone army (in fact, create the clone army due to the fake threat from the droid army), to divert attention, make the Jedi busy, and eliminate them in that one swift blow.
See here for more detailed information
Utilizing the attack by Mace Windu as a pretext, Palpatine issues Order 66, a pre-arranged military command ordering all Clone Troopers to execute their Jedi commanders in the event that the Jedi "betray" the Republic. Darth Vader leads the raid on the Jedi Temple with the 501st Clone Trooper Legion and kills all the Jedi in the Temple, including Jedi younglings living in the Temple.
Here's my reading of this: the Clone Troopers, although actually "picked up" by the Jedi and later lead by Yoda, belonged to the Republic (which was hardly likely to give the Jedi their own private army). Thus, their chain of command went up through the Jedi commanders to the chief executive elected by the Senate to lead the Republic in its times of trouble - Chancellor Palpatine. In any normal situation, they might be expected to be loyal to their generals; but as Juan pointed out above, these guys were bred for obedience. So they would obey their commander-in-chief - Palpatine - over any orders given by the Jedi.
According to recently launched Star Wars: The Clone Wars - The Lost Missions (Unfinished Season 6), it was an inhibitor chip embedded inside clones' brains which made the clones anti-Jedi at the time of Order 66.
The inhibitor chip belonged to Count Dooku and Lama Su agreed for this implant under a secret Protocol 66 which was a fail-safe against rogue Jedi.
Like some previous answers, Mine comes from Star Wars: The Clone Wars series. Season 6. I forgot the exact name of the episode, something like "The Lost."
EXTREME CAUTION!!! GIVE-AWAY AHEAD!!! READ AT YOUR OWN RISK OF SPOILERY!!!
Anyways, in this episode, it begins with a trooper that has a malfunctioning organic chip in his brain. His malfunctioning chip causes him to think Order 66 has already been issued, and he betrays a Jedi in the middle of battle. He later regains his former self, and is brought to Kamino for inspection. A friend of his comes with him, but is not permitted in the area. Later, they find out what did it, and don't tell the trooper who came with the patient. The trooper sort of "goes rogue" and attains a friend who is a Droid that did the procedure. They find the chip, and find out what it does. He gets the chip taken out of his own brain, and tries telling everyone what it does. Everyone thinks he's crazy, and he ends up getting accidentally killed by a fellow soldier. Sad death and blab blah blah. My point is, it was a tumor-like programmable chip implanted into their brains. They pretty much lost their will to obey there generals when Mr. Ex-Chancellor/ Emperor said "Execute Order 66." Im sure you've got the memo.
Here's the catch the reason why is because, if you have ever seen star wars the clone wars tv series season 6 the lost episodes is that here come the spoilers.Chancellor palpating and Tyranus/Dooku, had been with the kaminoens side all along and thought that they were Jedi so they ordered chips to be planted into every clone ate very young stage this is why when chancellor palatine said execute order 66 they all followed cause they were brainwashed into thinking that the Jedi are the SITH and the SITH are the Jedi thus the reason why they betrayed it was a brainwash command
So basically, when Palpatine created the Grand Army of the Republic, he ordered his apprentice, Count Dooku/Darth Tyrannus to put a chip inside of each of the clone troopers. What the chip did was that when the Chancelor tells them to execute "Order 66", they will see all the Jedi as traitors of the Republic and try to destroy them. Even so, there were some clones that disobeyed Order 66 because they had their chips previously remove (like Captain Rex, for example). If you watch the animated series, the Clone Wars, you will learn a lot more about Order 66 and all the clones.