The army as it's constructed in Turkey, has the duty to react if the secular values of the country are in danger. Erdogan has more than done that so they reacted. Just because Erdogan was voted (whether and how much the elections were fixed is another matter) doesn't give him the right to use power unconstitutionally and without reason. The army was the only thing standing between Erdogan legitimizing his islamist views. He was already placing his people at the higher chains of command and subtly changing the unity of the army and insuring that if a coup was made, the army wouldn't rise against him.
The trouble is, they have over 600,000 active military and the coup didn't have even 5% of that.
Had half the army rose against Erdogan, he would have been taken down yesterday. In a better organized coup, they would have seized Erdogan and/or the remains of his cabinet, and none of the islamists screaming Allahu Akbhar yesterday would have been on the streets.
The will of the people is a complex term. Even if 50% of the Turks are pro-islamist and anti-secularism doesn't mean that their breaking of the constitutional right of everyone else should be tolerated. That's where the army used to step in.
Now, with this failed attempt, Erdogan can be in total control and closer to the totalitarian regime he wants. Anyone against him will be deemed a traitor and arrested or worse. And no one can do anything about it.