He retired because he wasn't needed anymore after taking the fall for Harvey Dent. He told Gordon in the hospital dialogue, that "The Batman wasn't needed anymore. We won." This is because the goal of Batman was to be a symbol that inspired people to fight Gotham's criminal and corrupt, as stated Batman Begins. Since most of the cops were corrupt, Batman had to do this by vigilante means. Batman did this by waging war on organized crime--the root of the criminal and corrupt.
By The Dark Knight, he inspired both the copycat vigilantes and Harvey Dent to fight the criminal and corrupt. Bruce preferred the latter over the former as he told Alfred that "[the vigilantes] weren't what I had in mind when I wanted to inspire people". Bruce believes that Harvey was "the first legitimate ray of light in decades" and "the symbol of hope I could never be", since Harvey was using legal means to fight the criminal and corrupt and his prosecution managed to what Bruce called "lock up half of the cities criminals [without wearing a mask]". Since already 549 of the 1000 criminals were now locked up--allowing what Dent called "18 months of clean streets" for the 549 criminals was the whole mob (Lau, the mob's last money launder, when talking to the mobs, said "everyone's money would be at stake" for the mob laundered all their money to him; but he surrendered all his clients to Dent), Batman was soon not needed as the police can easily handle the remaining street criminals. Also, The Dark Knight further explains the temporary nature of Batman; Natacha in the dinner scene mentions "Gotham needs elected officials like you, not one who thinks he is above the law. Bruce then agrees by saying "exactly". But then Harvey, believing in vigilantism, said, "Who appointed the batman? We did. All of us who stood by and led scum take control of our city". But Rachel said, "But this is a democracy, Harvey." Harvey then said, "When their enemies were at the gates, the Romans would suspend democracy and appoint one man to protect the city. It was not considered an honor, it was considered a public service." So, when a enemy, like organized crime or a terrorist, is attacking Gotham, there must be a suspension of democracy since only a vigilante like Batman can effectively beat it. But, Rachel rebutted with, "Harvey, the last man to be appointed to protect the republic was named Caesar and he never gave up his power." So, after Batman defeats organized crime, he must give up his power or else he will be corrupt. Harvey then agrees with this and said, "Fine you either die a hero or live long enough to see yourself become a villain". And since Batman indirectly defeated organized crime by having Harvey's prosecution lock up the whole mob, Bruce then honors what Harvey said by telling Rachel in the Penthouse party that he was going to retire soon.
But then the mob leaders used bail to get out of jail and hire Joker, who then caused Harvey to go on a killing spree. According to Gordon, because of Dent's murdering spree, "All of Harvey's prosecutions, everything he fought for...undone." The mayor warned Harvey that in 18 months of jail, the mob would try to use any dirt on Harvey to undo his prosecution during the appeal process. Thus, if the "criminals can get anything on [Harvey], they would be back on the streets." Batman reiterated the same thing to Dent after he caught him interrogating the insane man in the dark alley. Plus, according to Joker, if the people of Gotham find out about his murdering spree, they would "[lose] their minds". That would probably create freaks like the Joker. So, Batman had to sacrifice his reputation to keep the prosecution and to keep Gotham's sanity. Since no one knew of Harvey's crimes, the mob would fail at appeal and not leave jail. With the whole mob in jail, half the mob's money burned, mob bosses dead--leaving the mobsters vulnerable and eliminating financial incentives for cop corruption--and the Joker captured, the remaining criminals on the streets would have trouble rebuilding the organized crime machine and would be arrested if they tried so. And with the Dent Act eliminating parole, the mob is locked up for good along with any additional criminals later arrested, thus deterring organized crime from bouncing back.
In conclusion, by inspiring Dent, the whole mob was jailed under his prosecution, then jailed for good--eliminating organized crime--under the Dent Act. Thus, Batman was no longer needed and Bruce could retire. Some viewers might note that The Dark Knight suggested Batman couldn't retire since Joker told Batman in their last encounter "I think you am I are destined to [fight each other] forever". However, Batman rebutted him by saying "You'll be locked up in a padded cell forever". Joker then said "Maybe we can share it". Thus, this suggests Joker would be locked up forever since he admitted to it. Also, some viewers might think Gordon saying "we will hunt him because he can take it" means he can't retire. Yet, in the 3rd film's hospital scene, Gordon seemed confused that Batman retired since he said "we were in this together. . . and then you were gone" He apparently misunderstood what Batman told him earlier of "you will hunt me. . .because that needs to happen". Batman meant to chase him that night so he can be viewed as a criminal, not for him to keep fighting crime. Otherwise, he helping capture any remaining criminals will bring suspicion to the public and cops that Gordon framed Batman, cause chase scenes on him so criminals would escape like what happened with the stock heist chase, and undermine Dent's martyring that inspired the public and cops to change for the better to fix the city.