WASHINGTON — By the time the campaign tracked down the small-city Indiana mayor, Bill Clintonwas in a lather. Senator Hillary Rodham Clintonhad lost the North Carolina primary that evening and was eager to offset it with a win in Indiana. But a vote-counting delay in one county threatened to rob her of a prime-time victory speech.

The Clinton campaign called a supporter for help. "I’ve got an angry president here and a candidate who wants to know whether or not she won," a local campaign representative told the mayor, Thomas McDermott Jr. of Hammond, Ind. Mr. McDermott could hear Mr. Clinton railing in the background. "It’s not very often you basically have a former president yelling at you to get the numbers out," he recalled.

The yelling was for naught. Mr. McDermott said he had no control over the vote count and, in the end, the late results cemented a negative narrative for an evening dominated by the North Carolina defeat with little attention focused on the eventual Indiana victory. The night of May 6 became the moment that Mrs. Clinton’s desperate comeback bid for the Democratic presidential nomination finally crashed against the reality of delegate math. All she had left was the perception of momentum, and suddenly, that was gone.

Hers was a campaign of destiny that fell achingly short, garnering nearly 18 million votes in her quest to become the first woman to hold the presidency. "Although we weren’t able to shatter that highest, hardest ceiling this time, thanks to you, it’s got about 18 million cracks in it," Mrs. Clinton said as she ended her campaign on Saturday.

Yet while she emphasized its trailblazing nature as she exited the race, her campaign also represented a back-to-the-future effort to restore the Democratic dynasty of the 1990s that could never quite escape the past. Although Mrs. Clinton proved a more agile candidate than many had expected, she built a campaign that was suffused in overconfidence, riven by acrimony and weighted by the emotional baggage of a marriage between former and would-be presidents.

As she flew from town halls to rallies on the road, she did little to stop the infighting back home among advisers who nursed grudges from their White House days. The aides grew distracted from battling Senator Barack Obamawhile they hurled expletives at one another, stormed out of meetings and schemed to get one another fired.

The Clintons struggled to adapt their successful formula to a new era against a new kind of opponent. They found their message of hope and change co-opted, and they found it hard to break out of the news media’s old image of them. Mrs. Clinton variously tried presenting herself as the friend having conversations with the American people, then the experienced hand and tough warrior before settling on heroine of the working class.

While Mr. Obama had mastered Internet fund-raising, it took Mrs. Clinton a year to do the same.

And as they tried to master a new political era, the Clintons demanded loyalty from those who once surrounded them and felt betrayed by people they assumed would be with them again.

"What hurt them was their sense of entitlement that the presidency was theirs and all the acolytes should fall in line," said Gov. Bill Richardsonof New Mexico, a former Clinton cabinet officer who endorsed Mr. Obama only to be branded a Judas by James Carville, the architect of Mr. Clinton’s original rise to power. "Instead of accepting it, they turned on the acolytes. It was their war room mentality, to attack when something doesn’t go their way, and it just reminded me of the old days."

In those days that mentality combined with grit and perseverance usually proved enough to win the day. If Gennifer Flowers, Newt Gingrichand Monica Lewinskytaught the Clintons anything, it was never to give in, no matter how many people told them to. This time, it was not enough, as Mrs. Clinton’s three-month effort to salvage her campaign after a cascading series of defeats in February fell short.

"Bill and I share a character trait of being determined and committed and not easily deterred or discouraged," she said in an interview in the waning days of the race. Asked about onetime friends who had abandoned her, she said, with a note of resignation in her voice, "That happens in politics."

These past three months played out with classic Clintonian drama. Her staff conducted rival polls while debating how much to campaign in North Carolina. Unlike her opponents, Mrs. Clinton refused to make solicitation calls to donors and had to be talked into calling the party officials known as superdelegates. Aides busily blamed one another for strategic mistakes that put her so far behind.

By many metrics, Mrs. Clinton actually won those three months. She beat Mr. Obama in the popular vote in 9 of the final 16 contests, collecting 600,000 more votes than he did along the way and racking up 509 primary and caucus delegates to his 472 in that period.

She found a groove as the candidate of the 1990s establishment finally reinvented herself as the populist champion. And she tapped into a deep well of support, particularly among women, that leaves her a formidable force in American politics.

"In the last three months, she just relaxed and let it rip," said Gov. Edward G. Rendellof Pennsylvania, a key Clinton ally. "She became almost a Hubert Humphrey, a happy warrior, and people responded to it."

It was politics on the edge. "The gun was to our heads many Tuesdays," said Terry McAuliffe, her campaign chairman. "If we didn’t win, we were dead. And we kept winning."

The old team
As Mrs. Clinton assembled her campaign to take back the White House, she brought together much of the old team, led by her chief strategist, Mark J. Penn, who had orchestrated her husband’s 1996 re-election. Just as they did in 1992, the Clintons were offering two for the price of one. As Mr. Clinton surveyed the field, he could not quite believe that an upstart, inexperienced senator from Illinois could be a serious alternative to such an accomplished figure as his wife.

The campaign was built on the assumption of overwhelming force. Strategists believed that the first four contests would be decisive and that she would wrap up the nomination by Feb. 5, when more than 20 states were to hold nominating contests.

Mr. Penn shaped a message that she was "ready to lead" a nation "ready for change," talking in early meetings about her need to spark a "movement" and dismissing Mr. Obama as a glamorous personality who would not connect with working-class voters the way she could, campaign officials said. "He may be the J.F.K. in the race," Mr. Penn told Mrs. Clinton last year, according to an insider, "but you are the Bobby."

Backed by Bill Clinton, Mr. Penn pushed for aggressive attacks on Mr. Obama, something other advisers resisted. At one point, Mr. Penn argued that Mrs. Clinton should find subtle ways to exploit what he called Mr. Obama’s "lack of American roots," referring to his Kenyan father and his childhood years in Indonesia and even the offshore state of Hawaii, the campaign officials said. Mr. Penn recommended that Mrs. Clinton own the word "American" — she should talk about the "American century" and her "American Strategic Energy Fund," and so forth. She should add flag symbols to her logo, he suggested.

Along the way, though, the campaign succeeded in defining Mrs. Clinton as a leader but not as an agent of change, and it hesitated in attacking Mr. Obama, who became the one leading a movement. Her logo was adorned with a flag, but her energy fund remained just an energy fund. Her strategists underestimated Mr. Obama’s strength and spent too much money before the voting even began.

In a March 2007 memorandum summing up the campaign’s consensus "key assumptions," the Clinton adviser Harold Ickeswrote that Iowa would be better for Mrs. Clinton than New Hampshire and projected that the campaign would raise $75 million in 2007 with $25 million left at the end heading into the first contests. In reality, she finished third in Iowa while winning New Hampshire. The campaign raised $100 million in 2007 but spent so much in Iowa that it was broke soon after the new year.

The Clinton team that had been so successful in the 1990s arrived at that moment bearing all the resentments of the old days. Mr. Penn, a sometimes brusque number cruncher with centrist corporate sensibilities, had few friends inside the campaign other than the candidate and her husband. Mr. Ickes, a bare-knuckled liberal friend of labor, had despised Mr. Penn since their days in the Clinton White House and did nothing to hide it, regularly mocking "our vaunted chief strategist" and at least once engaging in a profanity-laden shouting match with him.

There were other fault lines. Aides to Mrs. Clinton took umbrage at Mr. Clinton’s freelancing and deemed his office uncooperative — at one point, they complained, his people would not allow one of her people to ride on his plane to campaign stops. His aides, on the other hand, stewed over what they saw as her people’s disregard for the advice of one of this generation’s great political minds and bristled at surrendering control of his schedule.

The squabbling got so bad that it spilled into public view. "This makes me sick," Robert B. Barnett, Mrs. Clinton’s lawyer and debate coach, wrote in an e-mail message to top campaign advisers, according to someone who read it. "My message is simple: Stop it, please."

As for Mr. Clinton, he boiled with resentment that a candidate with as little experience as Mr. Obama was given what he considered a free pass by the news media. Yet his tone struck some as dismissive. When Mr. Clinton referred publicly to Mr. Obama as a "kid," Representative James E. Clyburn, Democrat of South Carolina, recalled in an interview that a fellow black congressman said, "I don’t know why he didn’t just call him ‘boy’ and get it over with."

In private, Mr. Clinton was making matters worse. On the night of the South Carolina primary, Mr. Clinton called and Mr. Clyburn said he told him to tone down his rhetoric against Mr. Obama. Mr. Clinton responded by calling him a rude name that Mr. Clyburn would not repeat in an interview. Mr. Clinton called back a few days later for what Mr. Clyburn called "a much more pleasant conversation," but the damage was done. "Clinton was using code words that most of us in the South can recognize when we hear that kind of stuff," Mr. Clyburn said.

A new landscape
This was no longer an environment the Clintons were accustomed to. Black voters had always been the most loyal part of Mr. Clinton’s base, and the accusations of racism wounded him deeply. The Clintons had little experience with caucuses in the 1990s and failed even to compete in most states holding them this year, allowing Mr. Obama to rack up a mass of uncontested delegates. Rather than sealing the nomination for Mrs. Clinton, the Feb. 5 coast-to-coast voting led into an 11-contest, monthlong losing streak.

Mrs. Clinton dumped her campaign manager, Patti Solis Doyle, who had been with her since 1992, and the two have not spoken since. Maggie Williams, the first lady’s chief of staff in the 1990s, was brought in to take over. Mrs. Clinton finally agreed to attack Mr. Obama in a more sustained way and scratched out victories in the Ohio and Texas primaries on March 4 to keep her bid alive.

The day of those votes, Mr. Clinton called Representative Jason Altmire, a freshman Democrat from Pennsylvania, the next state to hold a primary. "You could hear it in his voice how happy he was," Mr. Altmire recalled. "He said, ‘Now we’re coming to Pennsylvania and I know you might be leaning one way or another. I think you should keep your powder dry and hopefully we can win your support.’ "

The campaign shifted to a contest for the superdelegates, or party elders and elected officials like Mr. Altmire who can vote at the convention. Mrs. Clinton was too far behind to catch up to Mr. Obama among delegates selected by primaries and caucuses, so she hoped to persuade the superdelegates that she would be the stronger candidate in the fall. Only then did she agree to start calling superdelegates personally, something Mr. Obama had been doing for months.

Behind the scenes, Howard Dean, chairman of the Democratic National Committee, was pushing other superdelegates to announce their choices as quickly as possible in hopes of settling the nomination without added delay. Some Clinton aides viewed Mr. Dean as sympathetic to Mr. Obama and suspected his motives.

Tensions boiled over at a meeting in April between Mr. Dean and fund-raisers for the two campaigns at the Upper East Side apartment of the prominent Clinton supporters Steven Rattnerand Maureen White. What was supposed to be a moment of unity quickly deteriorated when one of Mrs. Clinton’s national finance chairmen, Hassan Nemazee, confronted Mr. Dean about the disputed Florida and Michigan primaries.

"I said to him, ‘It seems to me that you as the chair need to exert some leadership and produce some resolution to this problem,’ " Mr. Nemazee recalled. The two argued over Mr. Dean’s reply that Mr. Nemazee was trying to force him to choose sides, Mr. Nemazee and party officials said.

The campaign swung in unpredictable directions during the seven-week campaign in Pennsylvania. Radical sermons by Mr. Obama’s minister generated days of cable television coverage, but Mrs. Clinton’s false account of an under-fire trip to Bosnia stepped on her momentum. Mr. Penn pushed to go after Mr. Obama more directly for his association with the minister, the Rev. Jeremiah A. Wright Jr., but Howard Wolfson, the communications director, Mandy Grunwald, the media chief, and others resisted.

The rancor within the campaign escalated a few days later when Mrs. Clinton learned that Mr. Penn had met with Colombian officials as part of a contract to promote a free trade pact that the candidate opposed. Her aides fumed. One adviser, Tina Flournoy, walked out of campaign headquarters in anger. Mr. Penn’s many enemies seized on the chance to get him demoted. Colleagues rejoiced. "People felt like the sun came out a little more," said one. Geoff Garin, another pollster, and Mr. Wolfson took over the direction of the campaign, although Mr. Penn remained part of the operation.

While Mr. Penn had pushed to go on offense against Mr. Obama, seeing that as the only way to change the dynamics of the race, Mr. Garin steered in the other direction. "There were lots of people who spent a lot of time thinking about what to say about Barack Obama and not enough people waking up every morning thinking about how to make the case for Hillary Clinton," he said in an interview.

As the race wore on, Mr. Clinton played a growing role in shaping strategy. Flying to events or at the local campaign headquarters, he would pore over maps of Pennsylvania with Governor Rendell, pushing for more time and resources in the Philadelphia suburbs. He crisscrossed the state, hitting small towns and pressing superdelegates.

At one point, the former president visited Mr. Altmire’s district, giving what the congressman called "the hard sell."

While riding with Mr. Clinton in his car to an event, Mr. Altmire said, he asked how Mr. Obama’s learning curve at the White House would stack up with that of the former president, who was 46 when he took office. "I made a lot of mistakes when I started out," Mr. Clinton replied, according to Mr. Altmire. "And I did some things in office that were politically naïve, and I would have a fear that Senator Obama would have the same experience."

On election night, Mr. Clinton grew playfully competitive with his wife over who had done more events or had had more impact, Mr. Rendell said. Mrs. Clinton was superstitious and rarely watched election night coverage, but in the hotel suite, Mr. Rendell showed her husband county-by-county returns.

"The president wanted to know exactly what the returns were in the places he had been and Hillary hadn’t been," Mr. Rendell said. "He kept showing Hillary, and she would laugh."

Election night brought home the varied complex personal and political dynamics at play. Mr. Penn, once the most influential voice in the Clinton universe, showed up at campaign headquarters outside Washington to watch the returns but virtually no one would talk with him and he left early.

Mrs. Clinton handily beat Mr. Obama in Pennsylvania on April 22, but she could not translate that into gains with the critical superdelegates. The next day, she met with Mr. Altmire. She had won his district by some 31 percentage points and assumed that he would now commit his convention vote as a superdelegate to her. But he still refused.

"I think that was the frustration they were experiencing in that campaign," Mr. Altmire said. "They kept winning state after state and they expected others to start turning their way and it just didn’t happen."

Losing while winning
For Mrs. Clinton and her campaign staff, it was a surreal period. With each win, pundits would tell her to get out. The former president and his wife’s strategists became convinced that the news media, suffering from sexism, Clinton fatigue and Obama mania, were unfairly trying to hasten the end of their campaign. One day around a conference table, a group of advisers burst out in angry defiance. They started calling out the big states she had won — California, New York — and fired each other up.

"It just became, ‘You get out! Look at these states. You get out,’ " recalled one person in the room. "It got everyone pumped up."

Mrs. Clinton recognized the odds. But she was being encouraged by emotional supporters along the rope lines and came to believe she had an obligation to stay in, aides said. At every stop, someone would say, ‘Don’t you quit!’ " and aides said she internalized the message. "The psychology of it all is very complicated," one said. "I’m sure you don’t want to slow down because once you do, you start to think about things."

Advisers shied from suggesting she quit. "You’re a persona non grata if you bring up getting out," another aide said. "It just wasn’t talked about." But the real mission remained unclear. "What’s peace with honor here?" the aide asked.

If Mrs. Clinton ever thought about giving up, she seems to have kept it to herself. "We kept winning — if you’re winning, why should she leave?" asked Mr. McAuliffe, the campaign chairman and a close family friend. She never expressed doubt in front of him. "She never did. Never, never, never."

Mr. McAuliffe served as morale officer, regularly visiting headquarters and taking dejected aides to dinner. His feisty, manic television appearances became so ubiquitous that aides developed "Terry Bingo" with 25 boxes listing his most common lines of spin — "More electable," "Can still win" — and marked the boxes as he uttered them again and again.

Mrs. Clinton’s last real chance to change the shape of the race would come in Indiana and North Carolina. She started eight points down in internal polls in Indiana, which would prove to be the first state she would win after trailing.

North Carolina was the question mark. Mr. Clinton, unwilling to give up on his native South, believed they could whittle down her double-digit deficit and insisted on spending more time there. Mr. Garin took polls and reported back in an April 25 e-mail message that "we are on track to narrow this to single digits." Mr. Penn argued it was not possible and took his own shadow poll to prove his point.

On election night, North Carolina proved far beyond her grasp, and the Indiana results were lagging from a pro-Obama county. When Mrs. Clinton’s aides looked up at the television and heard what Tim Russertwas saying on MSNBC, they realized they were losing the perception battle. "She did not get the game-changer she wanted tonight," Mr. Russert said on air.

No more illusions
Deep in debt and no longer harboring even illusions of winning the nomination, Mrs. Clinton stopped attacks on Mr. Obama to avoid alienating him or the party. With only a handful of primaries left, Mr. Clinton began focusing on how to win as much of the popular vote for his wife as possible. "He wanted to at least put her in the position of being the vice president, and that was one way to do that," said an adviser.

Mrs. Clinton’s elation at each new victory was stemmed by some painful new setback. She crushed Mr. Obama in West Virginia. But as she celebrated, Mr. Obama upstaged her by appearing in Grand Rapids, Mich., the next day with a surprise endorser, former Senator John Edwards.

Mrs. Clinton noticed, however, that Elizabeth Edwardsdid not join her husband. Mrs. Edwards in recent months had grown to like Mrs. Clinton, an Edwards adviser said, and so the campaign reached out to see if she might back the New York senator.

Mrs. Edwards would not go that far. But the disaffection of other women over the pressure on Mrs. Clinton to step aside only stiffened her determination to press on. She received angry messages on her BlackBerry from friends like Ellen R. Malcolm, the president of Emily’s List, an abortion rights group that supports like-minded women seeking election. Ms. Malcolm said she vented in an e-mail message about how the news media were unfairly diminishing Mrs. Clinton’s victories.

Joe Andrew, a former Democratic Partychairman who had switched allegiance to Mr. Obama from Mrs. Clinton, faced the wrath of her supporters firsthand when he drove up to the Washington hotel where party officials were meeting last weekend to resolve how to count Florida and Michigan delegates. Protesters shouting "traitor" descended upon his Chryslerminivan, denting it with punches and kicks, he said.

The tensions fed a sense of defiance within the campaign. The Friday before Memorial Day, an aide was dispatched to the New York offices of People magazine to dispute a single word attributed to Mr. Clinton that left an impression of female frailty. The magazine quoted him saying Chelsea Clinton"bawled" after her mother’s loss in Iowa; the campaign said that Ms. Clinton had not cried and maintained that he had said "appalled." The quotation stood.

The withdrawal plan
By last week, though, anger had given way to resignation. Even before the final primaries on Tuesday, aides said Mrs. Clinton knew she could not continue. But she told them she would not concede that evening in the college gymnasium where she was to give her speech celebrating victory in South Dakota. She and her supporters, she told aides, had earned the right to their own day, and she planned to take two weeks to think through her options.

The next day, though, Democratic supporters in Congress pressed her on a conference call to give up quickly. She gave in, hung up and asked top advisers to prepare a plan to withdraw. They met with her at campaign headquarters, where every member of her inner circle recommended she pull out and endorse Mr. Obama without preconditions or negotiations — every member except Mr. Penn, who said she should hold out for concessions.

But Mrs. Clinton was, at last, ready to call it quits and switch focus to the general election, two aides recalled. "Let’s get on with it," she said.