As with most things, it's a bad idea to view the situation in Turkey in black-and-white terms. There are no good guys and bad guys, and regardless of the outcome (government wins vs military wins), it probably will have disconcerting consequences.
I'm going to try to cover the facts, and I'll get into speculations and my own opinions later.
First: the government appears to be back in control of the country. Many members of the military surrendered, and some may have been killed. Nearly 3000 have been arrested (unless that number has gone up since the article I read was printed). Eight soldiers fled in a helicopter, and requested asylum in Greece. They were, I think, arrested when they landed. Turkey wants to extradite them; some say the Greek government will process the asylum request in the usual way, while other reports say that the Greek Foreign Minister plans to return the soldiers "promptly".
More than 200 people were killed in the coup attempt, on all sides. Hundreds more were injured.
Arrest warrants were issued for 2745 judges, presumably for connections to the attempted coup. President Erdogan has said he wants to reintroduce the death penalty.
The president also said, "This uprising is a gift from God to us because this will be a reason to cleanse our army."
Here comes the speculation: who was responsible? The government has been quick to point the finger at an imam, Fethullah Gulen, and those loyal to his movement (sometimes referred to as hizmet). Gulen, who is presently in exile in the United States, has vehemently opposed the coup attempt, expressed support for Turkey's democratically-elected government, and denied any involvement in the coup. As of right now, there's no evidence of Gulen's involvement, and his supporters are mostly in the National Police, and to some extent, the judiciary; they don't dominate in the military. Gulen's movement was declared a terrorist organization by the Turkish government in May. This might put the US government in the middle of things, should Turkey request Gulen's extradition.
This Vox article goes into a bit more depth on who Gulen is and what his movement is about. Having that background might help you understand all the players involved.
Kemalists— those secularists I mentioned yesterday, the ones typically associated with the military— may have tricked Gulenists into attempting a coup, knowing it would fail, but it would mean that the military would be purged of Gulenists as a result. Which appears to be exactly what is happening. I have no idea how credible this theory is.
Alternately: the Kemalists were behind the coup. Considering that Erdogan has been consolidating power and is increasingly authoritarian.
Another theory: Erdogan and/or people in his party/government staged the coup so that they could exploit the aftermath. And while they are taking advantage of the situation to purge judges and military officials, all while blaming political opponents, the coup attempt perhaps went a bit too far. The coup attempt was succeeding, but may have been stopped when Erdogan managed to get word out to people (via a broadcast of a cell phone on CNN Turk, in which the president delivered a message using FaceTime), urging them to defy the military, go out into the streets, and stop the coup. And they did. Maybe they would have regardless, but I think hearing the president (who is, I think, fairly popular) tell them to, helped. The actions of the citizens of Turkey, I think, were the reason the coup failed. Had this been a false flag operation, there are too many points of failure. What if the military fired on those civilians? (Some did, but in general, it seemed the military was reluctant to harm civilians.) What if people stayed home? What if the president had been unable to encourage them to oppose the coup? (Lots of things could have gone wrong just in trying to get the message out.) It seems too risky.
A bit more on who might be behind the attack from the BBC.
Regardless who might be behind the attack, the president's recent actions and comments worry me. I don't know to what extent Turkey's judicial system acts as a check on government power, but I see the removal of so many judges as the removal of either political opponents, or those who might strike down any laws he wants to pass. Like restoring the death penalty. I don't think it's a stretch to wonder if there are people the president wants to execute— political opponents, for example— who he will tie to the coup as a means of doing so. He may also consolidate more power behind himself, and they may not be much opposition right now. I'm in favour of democracy, and this… is looking a bit more authoritarian and moving in the direction of a dictatorship or something.
Things might also get messy for any of the various factions in Turkey. Gulenists are already being rooted out. I'm not sure what the Kemalists think. We've also got Kurdish factions. Then there's ISIL at the gate.
No heroes in this whole affair.