The affair was discovered during an FBI investigation, according to officials briefed on the developments.
Mr Petraeus renowned for taking charge of the military campaigns in Iraq and then Afghanistan, carried on the affair with Paula Broadwell, a reserve army officer, according to several US officials with knowledge of the situation.
The FBI discovered the relationship by monitoring Mr Petraeus' emails, after being alerted that Ms Broadwell may have had access to his personal email account, two of the officials said.
Ms Broadwell, who is 40 and was co-author of All in: the education of General David Petraeus, did not respond to messages seeking comment.
Mr Petraeus' resignation yesterday shocked Washington's intelligence and political communities. It was a sudden end to the public career of the best-known general of the post 9/11 wars, a man sometimes mentioned as a potential Republican presidential candidate.
His service was praised effusively in statements from politicians from both parties.
Mr Petraeus, who was 60 on Wednesday, told CIA employees in a statement that he had met President Barack Obama at the White House on Thursday and asked to be allowed to resign. Yesterday the president accepted.
Mr Petraeus told his staffers he was guilty of "extremely poor judgment" in the affair. "Such behaviour is unacceptable, both as a husband and as the leader of an organisation such as ours."
He has been married for 38 years to Holly Petraeus, whom he met when he was a cadet at the US Military Academy at West Point, New York. She was the daughter of the academy superintendent. They have two children and their son led an infantry platoon in Afghanistan.
Mrs Petraeus is known for her work helping military families. She joined the new Consumer Financial Protection Bureau to set up an office dedicated to helping service members with financial issues.
Mr Obama said the retired general had provided "extraordinary service to the United States for decades" and had given a lifetime of service that "made our country safer and stronger". He called him "one of the outstanding general officers of his generation".
The president said that CIA deputy director Michael Morell would serve as acting director. Mr Morell was the key CIA aide in the White House to President George Bush during the September 11 2001 terror attacks.
"I am completely confident that the CIA will continue to thrive and carry out its essential mission," Mr Obama said.
Administration officials said the White House was first notified about the Petraeus affair on Wednesday, the day after the US election. Mr Obama, who returned to the White House that evening after spending election day in Chicago, was not informed until Thursday morning.
The Senate and House of Representatives intelligence committees were briefed on Mr Petraeus' resignation only after the news was reported in the media, said a congressional staffer, speaking anonymously.
The resignation comes at a sensitive time. The administration and the CIA have struggled to defend security and intelligence lapses before the attack that killed the US ambassador to Libya and three others. It was an issue during the presidential campaign that ended with Mr Obama's re-election this week.
The CIA has come under intense scrutiny for providing the White House and other administration officials with talking points that led them to say the Benghazi attack was a result of a film protest, not a militant terror attack.
It has now become clear that the CIA was aware the attack was distinct from the film protests across other parts of the Muslim world.
Mr Morell, rather than Mr Petraeus, is now expected to testify at closed congressional briefings next week on the September 11 attacks on the consulate in Benghazi.
For the director of the CIA, being engaged in an extramarital affair is considered a serious breach of security and a counter-intelligence threat.
If a foreign government had learned of the affair, the reasoning goes, Mr Petraeus or the person with whom he was involved could have been blackmailed or otherwise compromised. Military justice considers conduct such as an extramarital affair to be possible grounds for court martial.
Failure to resign could also create the perception for the rank-and-file that such behaviour is acceptable.
Though Mr Obama made no direct mention of Mr Petraeus' reason for resigning, he offered his thoughts and prayers to the general and his wife, saying Mrs Petraeus had "done so much to help military families through her own work. I wish them the very best at this difficult time".