Socrates stood trial for impiety, fomenting treason, and "corrupting the youth of Athens" in 399 B.C., in the immediate aftermath of the Peloponnesian War, a disaster for Athens. Faced with these charges (which he denied on the grounds that his teachings did none of those things), Socrates refused to recant. He also refused to stop his teachings, or to leave Athens, which was the usual sentence conferred in similar cases. In fact, Socrates, ever the gadfly, used his trial as a sort of platform for criticizing the government of Athens by questioning the moral foundations of Athenian democracy. He was sentenced to death by drinking hemlock, which he did, surrounded by his students and other admirers, including Plato, whose Apology, Criton and Phaedo are the only contemporary sources for Socrates's trial and death.
First, we might note that Plato's Apology is not a verbatim transcript of the actual trial. Thus, although we can state the arguments Plato had Socrates use in the Apology, we cannot know the degree to which they resembled the actual arguments Socrates used in the real trial.
Socrates was accused of the crime of "asebia" or "impiety". Specifically:
[the accusation] asserts that Socrates does injustice by corrupting the young, and by not believing in the gods in whom the city believes, but in other daimonia that are novel ...
Socrates refutes the charge of corrupting the young in several ways. He argues that as he is just one person, he could not be solely responsible for corrupting all the young of the city. Next, he gives several examples of young men he has associated with who have improved because of his company. He also suggests, in the case of his young associates who were corrupted, they only became so when they stopped associating with him. Next, he differentiates himself from the sophists who take money from teaching.
For the accusation of impiety, he tries to explain the nature of his daimonion as a sort of inner voice rather than a god. He also does participate in the ordinary religious observances of the city.