In a statement, Mr Petraeus described his behaviour as "unacceptable" for the leader of the nation's main intelligence agency. Unnamed officials said Mr Petraeus conducted the affair with his biographer, Paula Broadwell. Mr Petraeus became CIA boss in 2011 after heading international forces in Iraq and later in Afghanistan.
He was the highest-profile military officer of the post-9/11 years, winning plaudits for his role running the "surge" in Iraq and implementing a counter-insurgency strategy in Afghanistan.
He left his command role in Afghanistan in mid-2011, resigning from the US Army to head the CIA after Leon Panetta became defence secretary. Shortly after Mr Petraeus resigned, reports emerged that the FBI had uncovered the affair during the course of an investigation into Mrs Broadwell.
The FBI was monitoring Mr Petraeus' email account to check whether Mrs Broadwell had access to it, administration officials were quoted as saying by the Associated Press.
Mr Petraeus' resignation came just three days after President Obama's re-election, and prompted a flurry of statements from the White House, intelligence community and Mr Petraeus himself. Announcing his decision to stand down, the former general was full of contrition.
"After being married for over 37 years, I showed extremely poor judgment by engaging in an extra-marital affair," Mr Petraeus said in a statement.
"Such behaviour is unacceptable, both as a husband and as the leader of an organization such as ours. This afternoon, the president graciously accepted my resignation."
Mrs Broadwell is a Harvard University research associate and PhD candidate at King's College, London.She has a military background, graduating from the same West Point Academy as Mr Petraeus. She is married to radiographer Scott Broadwell and lives in Charlotte, North Carolina.
She spent months embedded alongside the then-general in Afghanistan while researching her 2011 book, All In: The Education of General David Petraeus. The book was widely seen as a positive account of his leadership methods. Last week Mrs Broadwell wrote a piece for Newsweek entitled General David Petraeus's Rules for Living.
Mr Obama's statement said Mr Petraeus had "provided extraordinary service to the United States for decades", citing both his time as CIA director and service to the military.
"By any measure, through his lifetime of service David Petraeus has made our country safer and stronger."
"Going forward, my thoughts and prayers are with Dave and Holly Petraeus, who has done so much to help military families through her own work."
Senior Obama advisor David Axelrod said the president had not known about the Petraeus issue during the campaign, and that the former general had come to Mr Obama after the election to offer his resignation. James Clapper, director of national intelligence, thanked Mr Petraeus for his decades of service: "Dave's decision to step down represents the loss of one of our nation's most respected public servants.
"I have spent more than five decades serving our country - in uniform and out - and of all the exceptional men and women I have worked with over the years, I can honestly say that Dave Petraeus stands out as one of our nation's great patriots."
CIA deputy director Michael Morell will serve as acting director of the agency, the White House confirmed. Eventually Mr Obama must nominate a new director to head the agency, who will then need to be confirmed by the Senate. Mr Morell, who is well respected at both the White House and on Capitol Hill, also served as acting director following the departure of former CIA chief Leon Panetta.
The CIA faces a potential period of instability after Mr Petraeus' resignation, as it also deals with a budget squeeze and questions over its response to the attack on the US consulate in Benghazi.
David Petraeus: Career highlights
- Graduated from West Point in 1974
- Commander of Multinational Force Iraq, Feb 2007 to Sept 2008
- Commander of International Security Assistance Force (Isaf) in Afghanistan, July 2010 to July 2011
- Became CIA director in Sept 2011 after being confirmed by the senate 94-0