On the march to Baghdad in 2003, back when many Americans expected the Iraq war to last weeks and not years, a little-known two-star Army general named David H. Petraeus posed a question to a reporter accompanying him that would come to define the grueling U.S. combat mission there: “Tell me how this ends.”

Before long, it was a query that many began to pose about Petraeus himself. After refashioning the military’s strategy for fighting insurgencies, after picking up his third and fourth stars, after overseeing the troop surge in Iraq and running the U.S. Central Command, and after taking charge of the war in Afghanistan, Washington buzzed with speculation of his next move. Would he run for president? The questions continued even after President Obama appointed him CIA director. Would he be Mitt Romney’s running mate? Nothing, it seemed, was out of reach for America’s most-famous living general.

Now, we know how it ends for Petraeus.

His resignation Friday as CIA director because of an acknowledged extramarital affair aborts an almost four-decade-long career in public service defined by boundless ambition, political savvy and strategic acumen. And it almost certainly tarnishes the legacy of a man seen by many as the nation’s preeminent military leader in the post-Sept. 11 world, a commander who turned around the failing Iraq war and dealt the Taliban a bloody punch in Afghanistan.

He falls from a self-built pedestal that was based on more than battlefield heroics. As a general, his principal message to the troops under his command was not just about military tactics and high-concept strategy. He preached individual leadership above all else, often telling his charges that character meant doing the right thing when nobody was watching.

For Petraeus, a compact man with seemingly limitless energy, the race to the top began early.

Petraeus grew up outside the U.S. Military Academy at West Point, which he entered as a cadet in July 1970. “A striver to the max, Dave was always ‘going for it’ in sports, academics, leadership, and even his social life,” the West Point yearbook noted in 1974. A month after he graduated, he married Holly Knowlton, the daughter of the academy’s then-superintendent.

Petraeus quickly made a mark as a young officer, earning awards in almost every assignment. He received all three prizes awarded in his class at Ranger School, perhaps the Army’s toughest physical challenge.

Unlike most of his peers, he adroitly pursued opportunities that were not always the most exciting but would come to help him later. He chose to serve as an aide to four-star generals, carrying their bags and cultivating valuable friendships. He also carved out time for advanced education, culminating in a doctorate from Princeton University.

Two accidents almost ended his career. In 1991, he was shot in the chest with an M-16 rifle when a soldier tripped during a training exercise. While skydiving in 2000, his parachute collapsed while he was 60 feet off the ground; the impact shattered his pelvis.

Although the injuries had no discernible impact on his physical abilities — while in Iraq and Afghanistan, he regularly beat junior officers in foot races — Petraeus assiduously framed a hard-charging narrative of recovery. He frequently told the story of doing push-ups in his hospital room to persuade his doctor to let him leave, but he didn’t mention that occurred after he had been readmitted to the hospital because he did not follow orders to rest.

In 2003, he led the storied 101st Airborne Division in the invasion of Iraq. His soldiers engaged in fierce firefights along the Euphrates River valley before moving up to occupy the northern city of Mosul, where he won praise for ignoring foolhardy orders from the U.S. occupation administration in Baghdad that banned the reintegration of former Baath Party members. Posters in the division headquarters read: “What have you done to win Iraqi hearts and minds today?”

After bringing the 101st home, he returned to Iraq to take charge of the troubled effort to build a new army and police force. His efforts met with only limited success. In subsequent discussions about his wartime experiences, he frequently glossed over that period.

His next assignment seemed to his colleagues to be a career-ender—command of the U.S. Army Combined Arms Center at Fort Leavenworth, Kan.—but Petraeus used it to vault himself into the elite ranks of warrior diplomats by overseeing a rewrite of the Army’s counterinsurgency manual at the very moment America’s strategy in Iraq was adrift. Petraeus advocated the use of U.S. troops to provide security to Iraqi civilians, reasoning that reducing sectarian warfare would bring the conflict under control. His approach found favor with President George W. Bush, who authorized a troop surge to implement the strategy and sent Petraeus back to Baghdad—with a fourth star—to oversee it.

For much of his 37-year Army career, Petraeus had a mixed relationship with his Army colleagues. He had few close friends among his peers, who saw him as too ambitious and criticized him for spending more time as a generals’ aide than leading troops in the field. As a battalion and brigade commander, he was regarded as smart, hardworking and creative, but he was never seen as an inspirational leader.

In Washington, however, the Petraeus mythology took hold. He was feted at the White House. His appearances on Capitol Hill received live cable television coverage.

After Iraq, Bush gave him the Central Command. It could have been a sinecure, but Petraeus spent much of his time traveling in the Middle East and South Asia. When he wasn’t overseas, he didn’t go back to his home base in Tampa. He stayed in Washington, where he could more easily meet politicians and journalists for dinner.

When Obama fired Gen. Stanley A. McChrystal in June 2010, after the general’s aides made intemperate remarks to a magazine reporter, the president asked Petraeus to take over the Afghan war. Although Petraeus rode to fame on counterinsurgency strategy, he quietly shifted emphasis in Kabul, authorizing a massive increase in Special Operations raids and airstrikes intended to hammer the Taliban. Although he never admitted it in public, he effectively embraced a counter-terrorism approach that he had opposed during strategy discussions at the White House a year earlier.

It was, in many respects, vintage Petraeus. He wasn’t going to remain wedded to a strategy that wasn’t working. It also had the virtue of pleasing the Obama White House, which had long been skeptical of counterinsurgency.

Petraeus had hoped his Afghan service would lead Obama to select him as the next chairman of the Joint Chiefs of Staff. But the White House feared he would use that perch to oppose troop reductions in Afghanistan, so he was sent to the CIA, where he was given the reins of the agency’s covert global war against Al Qaeda, including a blistering drone campaign in Pakistan.

Top Obama administration officials figured the CIA job would keep him out of politics this year, making it difficult for him to run for president or even emerge as a vice presidential candidate. In the wake of Tuesday’s election, he once again found himself on many lists of possible GOP contenders in 2016.

By Friday, he was no longer even a long shot. His resignation reverberated across Washington with the power of a 2,000-pound bomb. Friends expressed dismay. Politicians voiced shock. Fellow generals were incredulous. Some who knew him wondered how he could have been so hypocritical. Others cast it as a forgivable lapse in judgment and predicted he would eventually reenter public life.

A few recalled his admonition about character and nobody watching. And they remembered the warning he often tacked on: “Someone is always watching.”

Greg Jaffe and Julie Tate contributed to this report.