Conventional wisdom says that democracy is the superior form of government.
If this is true, then factors might have contributed to earlier democratic states like Athens in 500 BCE and the Roman Republic failing?
I'll answer just the part about the Roman Republic, if that's alight for now.
The Roman Republic is probably best described as a pseudo-democracy of sorts. Its creation and initial set-up actually pre-dated Athenian democracy by a single year, though even until its dying days it was more of a "democracy for the privileged" than anything. Hence, Classical-period Athens is usually considered the world's first true democracy. Nonetheless, the Roman Republic was clearly a very successful model for a state. It was ultimately the lack of fail-safes and precautions against powerful consuls and leaders that led to its down-fall, and arguably the fact that only landowners had a say in the vote, or indeed only equestrians (members of the ancient noble families) could become senators or consuls. Your average peasant, born in Rome or otherwise had little say in the state.
Generally seen as the first real sign of the downfall of the Roman Republic was Lucius Cornelius Sulla's stint as dictator (to use the actual Latin word). Dictator was a special privilege granted to a military leader (general) by the Senate -- often a past consul by nature of the office -- in times of great need. For example, during the Sack of Rome in the 4th century B.C. by the Cisalpine Gauls (of northern Italy). It was however used extremely sparingly until Sulla was granted the position during the Roman Civil War of ~82 B.C. (the very worrying second one in the span of a decade). The senate relied on the honour of the general to relinquish the title of dictator when it was no longer required and return to normal life (of a senator often).
Fortunately at first, Sulla did this, though as we all Gaius Julius Caesar did not and craved power until his final assassination. Ironically, Caesar was known as a member of the populares (unlike his aristocracy-aligned rival Pompey [Gnaeus Pompeius] of the First Triumvirate), gaining widespread support from the working class, and thus facilitating the end of what small democracy there was, and the start of Octavian's tyrrany. I hope you can see from this that effecitvely the collapse of this pseudo-democracy was facilitated greatly by the lack of sufficient legal codes as well as increased internal strife within Italy, propagated by corrupted and varying degrees of oligarchic control. There were always too many loopholes in Roman politics and law to allow power-hungry men to wield disproportionate control. One might say the Roman Republic was a time bomb ready to explode (though not as disastrously as the Roman Empire of course!).
A lecture summary for a history course on Classical History in the Purdue University gives a good summary and highlights four main causes (in the lecturer's view, though largely accepted by historians I'd say):
The rise of the popular tribunes -- late 2nd century B.C.
The rise of the private armies -- most notably by Garius Marius (namesake of the notable Marian reforms of the military) and Lucius Sulla (the famous twice-dictator).
The First Triumvirate -- as mentioned above, consisting of Julius Ceasar, Pompeius Magnus, and Lucinius Crassus. Note that the second triumvirate effectively occurred after the fall of the Republic.
The granting of Caesar's dictatorship -- a 10 year period in which he was meant to restore the Republic, but instead laid the foundations for the Imperium beginning with Octavian (Augustus).
All contributed to increase oligarchy and eventual pseudo-monarchy in the Roman Empire. The natural response to the many crises that occurred over the first century B.C. was to put power in the hands of great individuals; a dangerous move at any time, despite it having some logic. No doubt, the spiral towards collapse had begun long before Caesar's heydey. Worth noting: the first Emperor Octavian cunningly maintained the pretence of the Republic's continuation, gradually dropping it towards the end of his reign.
Now there are of course other explanations, and I have only touched on the surface here, for it is a highly complex subject, but I hope this gives some insight for you to go and read around the matter further, at least.
Rubicon: The Last Years of the Roman Republic, Tom Holland (2003)
Democracies were not necessarily more stable than other forms of government. Polybius describes a cycle of three forms of government - monarchy, then aristocracy, then democracy, then back to monarchy again (of course, this was not always the case in practice). The important point to note is that each first degenerates into an inferior form (tyranny, oligarchy and mob rule respectively) before being replaced. Wikipedia gives a good overview:
Polybius considered the Roman Republic to have the best and most stable form of government - mixed government combining facets of monarchy, aristocracy and democracy, each being able to check the power of the others. However, less than a century after he wrote these words, the Republic was on its knees.
As Noldorin says in his answer, Rome wasn't particularly democratic and there was a long history of struggle between the rich and the poor. Appian traces the beginning of the end of the Republic to the assassination of the tribune Tiberius Gracchus, to his knowledge the first instance of the regular class struggles descending into internal conflict. Both Tiberius Gracchus' and others' proposals for the improvement of the lives of the poor met with vigorous and sometimes violent opposition by the wealthy, so the poor would have had little interest in maintaining a system that did not work for them.
The reforms of Gaius Marius introduced to the army soldiers the poorer parts of the citizenry who did not previously meet the land holding requirements. While nominally servants of Rome, their hopes of wealth and a comfortable retirement rested upon the success of their commanders not only in battle but also in securing lucrative political appointments. This opened the door to Marius and others after him to build personal armies to back their bids for power. Noldorin has covered the subsequent events well so I won't go over the same territory.
The democracy of Athens was briefly replaced by an oligarchy during the Peloponnesian War before returning to democracy. After losing the war, Athens camed to be ruled by another oligarchy in the rule of the Thirty, this time imposed by pro-oligarchy Sparta. It idnd't take long for Athens to again return to democracy. The later demise of this democracy was again due to external factors - conquest by Macedon and Rome.
Its not in any way a given that the desirability of a government should coincide with the length of time it manages to stick around for. Never-the-less, ancient democracies had pretty good track records as far as governments go. Sparta lasted for 400 years, Athens for 250, the Roman Republic for almost 500 years. If you include states with even more limited representation, Venice's government lasted for 1000 years, tho it changed a whole lot in that time and lost what little democracy it generated early on. Democracies have a much better track record for length than monarchies do (on average).
Ancient democracies didn't have the conception of a difference between constitutional law and normal law. Because of this their governments weren't stable and changed continuously.
Many democracies quickly changed to solidify the status quo, benefiting the new elites that had managed to rise up during the period of elevated representation. Sparta's Apella was undermined less than 100 years after it gave representation to about 3% of Sparta's population - a world record at that time in history. The power between the patricians and the plebians in the Roman Republic constantly shifted back and forth, ultimately leading to the merging of the patricians and the newly created plebian aristocracy. In Venice, the relatively representative Great Council was closed off to new entrants, again less than 100 years after it was created. Similar things surely happened in the Pala Empire, the Mali Empire, Sakai, and various other places where some representation temporarily flourished.
Some of these were essentially cities that controlled other cities. Sparta, Athens, and Venice were all conquered by a larger power in the end - tho only Athens still had its democracy by that time.
I wrote a whole article on ancient democracies, which in part, discusses this in more detail. Check it out here: https://governology.wordpress.com/2016/05/04/government-behind-us/