This is a big question! Treaties and alliances in the ancient world have been a subfield of study at least since Rudolf von Scala published the first volume of the Staatsverträge des Altertums in 1898. We have the full text of many treaties of alliance, not just because they were cited by ancient writers, but often because they survive as inscriptions on stone. Speaking just for the Greek world, the known record of military alliances stretches back to around 700 BC. No doubt the practice is much older than that.
The Greek word for a full alliance was symmachia and allies were called symmachoi - literally "co-fighters". This reveals both the origin and purpose of alliances between states. They were crucial in a world that consisted of hundreds of tiny political communities; it was impossible for small ones to withstand the aggression of bigger ones without help. So, from the earliest times, they made public agreements to support each other if it was to their mutual benefit, and often joined together in sprawling networks of alliances to gather as much support as they could.
The standard form of alliance in the Classical Greek world was the simple defensive alliance. These alliances were usually framed something like this agreement between the Athenians and the tyrant Dionysios of Syracuse (c. 368 BC):
Praise Dionysios the ruler of Sicily, because he is a good man with regard to the people of Athens and the allies. He and his descendants shall be allies of the people of Athens for all time on the following terms. If any one goes against the territory of Athens for war either by land or by sea, Dionysios and his descendants shall go in support as the Athenians call on them, both by land and by sea with all their strength as far as possible; and if any one goes against Dionysios or his descendants or what Dionysios rules for war either by land or sea, the Athenians shall go in support as they call on them, both by land and by sea as far as possible.
The inscription shows how formal and standardised these treaties were. There is painstaking symmetry in the obligations of both sides, precision in the description of the parties (not just Dionysios, but also his successors, if any), and the constant repetition of the clause "by land and by sea" to cover all bases. I could cite many other examples that follow the same principles and use the same words. These defensive alliances could be between individual states or, like the so-called Second Athenian League (c. 375-355 BC), they could be a standing agreement between a group of states that new members could apply to join under the stated terms. Sometimes the alliance was for a set period (typically 50 or 100 years), but often, like the example above, alliances were in principle made to endure "for all time."
If this was the normal kind of alliance between Greek states, there were also a few other types. One was the full alliance, which was typically expressed by the oath that the states involved would "share the same friends and enemies." This was not merely a defensive agreement to come to the aid of friends who were under attack, but a pledge to have a common foreign policy in both offence and defence. The Athenian-led Delian League (478-404 BC) was originally formed on this principle, with members throwing lumps of iron into the sea and swearing they would have the same friends and enemies until the iron floated to the surface (Pseudo-Aristotle, Constitution of the Athenians 23.5).
In theory, these alliances were the result of states realising their shared interest, and representatives might meet in council to determine policy. But in practice these treaties were often forced upon smaller states by bigger ones. The so-called Peloponnesian League was a full alliance between equals on the surface, but the terms of the treaties that bound individual states to this league show clearly that it was really an instrument of Spartan power: members were not just required to "share the same friends and enemies" as Sparta, but to "follow wherever the Spartans lead, on land and sea" (Xenophon, Hellenika 2.2.20). These states were only protected as long as they followed Sparta's orders.
Another form of alliance was the amphiktyony. This was a sort of regional pact between states to protect a shrine or sanctuary. The states involved might be otherwise independent and even hostile, but they nevertheless swore to march out in force against anyone who might violate the sacred site. The most famous of these was the Delphic Amphiktyony, which may have already been centuries old when the Athenian orator Aischines recorded its members' founding oath (On the Embassy 115):
The men of ancient times swore that they would raze no city of the Amphiktyonic states, nor shut them off from flowing water either in war or in peace; that if anyone should violate this oath, they would march against him and raze his cities; and if any one should violate the shrine of the god or be accessory to such violation, or make any plot against the holy places, they would punish him with hand and foot and voice, and all their power.
At any point, a given Greek state would probably be involved in several such alliance networks, and maintain additional alliances of its own. Whether these treaties held in wartime always remained to be seen, but all the major wars of the Classical period were wars between such alliance systems. Most Greek states were simply too small to survive on their own; they knew they needed powerful friends, and they had well-established treaty types to gain and keep them.