Why Socrates was really executed has been of some concern for philosophers and thinkers ever since the event, as it was easy to interpret his trial and execution as somehow showing that the world was inherently hostile to philosophy. Socrates died because he was accused of "corrupting the youth" of Athens during a time which was supposed to be dedicated to democratic consolidation; his corruption consisted of apparently turning prominent citizens against democracy by his teaching. This was despite the fact that Socrates always pledged allegiance to the state of Athens and later refused the opportunity to escape from his cell prior to his execution because he held that to obey the law was always right. At the heart of this seeming contradiction lies the reason for the execution of Socrates.
The most outstanding thing about Socrates as a philosopher is that he left us with virtually no positive commandments. He spent his life investigating concepts like justice, but each dialogue invariably concludes with Socrates saying that he has learned nothing. He got his kicks from questioning people's beliefs and highlighting the contradictions in them, or breaking them down into progressively smaller concepts that escaped easy definition.
His purpose was not to instruct them to see things a certain way - to replace their own concept of justice with his own - but to teach them what was essentially a method, a way of thinking about things. The Socratic method destroys every belief and value it comes into contact with because such things are not the product of rational analysis, and hence inevitably dissolve under it. No one can prove that their own concept of justice is correct, and as soon as we analyze it in theory we find that it is rooted in irrational beliefs. The activity of a philosopher like Socrates can hence be inimical to the things that keep society together - laws, customs, morals - because all he can do to them is undermine them by doubting them.
Socrates was even exceptional among philosophers in this regard because he never wrote anything down or tried to transform what he taught - that is, doubt - into a doctrine. His actual goal was to make people move closer to things he considered good, like justice or beauty or piety, by making them devote themselves to thinking about these things rather than just accepting the shadows of them that they saw around them, expressed in people's unexamined opinions. And he did this through dialogue, through interaction with people, by the method itself, not by transforming it into a book that would inevitably be cheapened and open to misinterpretation. His goal was not to leave an example to posterity - which is the goal of those who write books - but merely to exist in his own time and lead the best life which he could conceieve of; that is, the life of philosophy.
Socrates was hence said by Martin Heidegger to be the "purest" thinker of them all. But his activity was also eminently political. Socrates needed to interact with his fellow-citizens to do what he did. Because he knew nothing, he relied on learning the opinions of others to give himself food for thought; he needed people to dispute with.
Because Socrates did not aim at merely sitting alone and coming up with a concept of justice or piety from whole cloth, he actively relied on processing and destroying existing opinions about the concept; and because the contradictions in a concept like justice can never be resolved, he would always be a destroyer. And not only this, but he would forever carry out his activity in public, making the elders of Athens look stupid and undermining their authority all the while. One reason Socrates refused to escape from his cell was because he recognized that wherever he went, he would be persecuted; he could not escape and remain who he was, and he preferred death to having to stop being who he was.
The activity of philosophy thus viewed seems to have a tendency towards nihilism; if no meaning can ever be proven, then some people are encouraged to abandon the search for justice or piety altogether and to act immorally. This "is pretty much the opposite of what Socrates intended to achieve", as is demonstrated by the last part of our story, the fact Socrates refused to escape from jail. Socrates refused because he held that he had made an agreement with Athens to obey its laws by choosing to live there; he had always found Athens amenable to his lifestyle precisely because it was well-governed, allowing diverse opinions to flourish, and so he could hardly take himself seriously if he now decided to disobey the law by escaping from jail and fleeing. He would be a hypocrite, and Socrates had spent his life battling hypocrisy.
This parable shows that even Socrates, the man who spent his life thinking and destroying, has to have a place to live in the real world, which relies on the acceptance of some of the facts and beliefs of the world. What Socrates died as a martyr to was his belief in the need to always think and question everything around us and especially authority, but his death was brought about precisely because this activity was incompatible with his life in the small community of Athens, a life which he had accepted as the best available to him in Greece.
This seemed to introduce an insuperable contradiction into the life of the philosopher, who relies on politics but is always destructive of it. This has been addressed in various ways from Plato through to Nietzsche. Plato answered with his parable of the Cave, saying that the philosopher had to remove himself from the affairs of men as much as possible to remain safe. The parable of course amounts to an impossibility, as no person can remove themselves entirely from the company of others, who they must instead learn to live with.
Nietzsche, on the other hand, who regarded Socrates as an "enemy" because he destroyed values, tried to solve the problem by making the creation of values and not doubt the central preoccupation of the philosopher. But Nietzsche was fully aware that this creative vitality rested on the flexible meaning of concepts like justice, and the fact no agreement could ever be reached; unlike Socrates, who wanted to teach people to approach the concept through reasoning - even though they knew they could never reach it fully - Nietzsche wanted people to embrace the irrational basis of justice as an unavoidable fact of life. And so, in Nietzsche's view, the Athenians were right to execute Socrates, who threatened this basis; and this is the view that seems to have been adopted, on a superficial level, by Socrates himself.