There are three main possible explanations for the attempted coup that took place in Turkey.
The first is that this was a spontaneous coup organized by the army or the people who wanted to overthrow the government in place to render Turkey more "democratic".
The second is a false-flag operation instigated by Erdogan and his entourage in order to authorize, by repression, an increased clamping on the Turkish society.
A third, not to be overlooked, hypothesizes a coup fomented by the United States and their allies in the Middle East to expel the Turkish establishment that began to move closer to Russia. This is Alexander Dugin's hypothesis (and Erdogan's), who develops an analysis centered on the possibility of a parallel state in Turkey.