WASHINGTON — The night before he was fired, Attorney General Jeff Sessions was in high spirits.He was watching the midterm election results at the Justice Department with his wife and staff members, according to two people present, and enjoying a final, welcome evening of normalcy before President Trump abruptly ended his bumpy tenure on Wednesday as the United States’ highest law enforcement officer.While Mr. Sessions, 71, did more to carry out Mr. Trump’s agenda than almost any other cabinet official, delivering on immigration and combating violent crime and opioids, he also soured their relationship after only weeks in the position by recusing himself from the Russia investigation. The president, who has made clear that he expected protection from his law enforcement officials, viewed the step as a betrayal.“The one laudable thing he did was to recuse himself from the Russia investigation, and for that, he is being fired,” said Vanita Gupta, a frequent critic of Mr. Sessions and the chief executive of the Leadership Conference on Civil and Human Rights.Mr. Trump installed Mr. Sessions’s chief of staff, Matthew G. Whitaker, who had served as a liaison to the White House and gained the president’s trust, as acting attorney general.Though Mr. Sessions quickly lost Mr. Trump’s support, he focused on accomplishing his agenda. Mr. Sessions established a zero-tolerance policy for illegal immigration that filled courtrooms along the southern border. He cracked down on MS-13 gang members. And he gave federal prosecutors more resources to fight the opioid crisis.Some Justice Department employees came to worry that Mr. Sessions was so focused on his agenda that he politicized the work of rank-and-file lawyers, they have said in interviews. But they also said that they respected him for enduring Mr. Trump’s attacks and for pushing back when the president threatened to undermine the rule of law.“For Attorney General Sessions to go out there and carry out the president’s agenda — whatever you think of that agenda on the merits and the substance — he was doing his part to to carry out the job he was asked to do,” said Megan Brown, a former Justice Department lawyer who worked with Attorneys General Alberto R. Gonzales and Michael B. Mukasey.During nearly two decades as a senator, Mr. Sessions endorsed only one presidential candidate: Donald J. Trump.Mr. Sessions saw in Mr. Trump a candidate who was interested in the socially conservative and protectionist agenda that he favored and that slotted him as one of the most conservative members of the Senate. He became the first senator to back Mr. Trump, lending credibility to his 2016 campaign amid a heated Republican primary contest.In return for his loyalty, Mr. Trump appointed Mr. Sessions as attorney general — only to quickly come to regret the decision.Mr. Sessions’s recusal from the Russia investigation prompted more than a year of public complaints and private humiliations by Mr. Trump. “He took the job and then he said, ‘I’m going to recuse myself,’” the president said this year in a Fox News interview. “I said, ‘What kind of a man is this?’”An even-tempered former Eagle Scout and a Methodist, Mr. Sessions is unlikely to ever publicly attack the president in return.Even in private conversations, Mr. Sessions never criticized Mr. Trump, no matter how low their relationship had sunk, said Richard W. Moore, the United States attorney for the Southern District of Alabama in Mobile and a longtime friend of Mr. Sessions’s.“I don’t think he will say anything bad about him in the future, ever,” Mr. Moore predicted. “It’s not who he is.”Mr. Sessions tried at least twice to resign last year, though presidential aides stopped him.On the outs with the president, Mr. Sessions focused on reorienting the department toward the Trump agenda. He redefined civil rights, pulled back on oversight of the police and sided with restrictions on voting rights. Perhaps most prominently, his zero-tolerance approach on illegal immigration led to separations of families that set off a broad outcry and forced Mr. Trump to retreat.Mr. Sessions’s tough-on-crime agenda extended beyond illegal immigration. In his resignation letter, he touted the department’s high rate of prosecutions of violent offenders and firearms violations, as well as of immigration offenses.His efforts infuriated civil rights advocates. “He rolled back significant areas of progress,” said Ms. Gupta, who ran the civil rights division during the Obama administration.But Mr. Sessions won the backing of many law enforcement professionals.While demonstrators ringed the Justice Department this year to protest the Trump administration’s separation of immigrant families at the border, Mr. Sessions spent nearly an hour talking to the United States marshals and local and federal law enforcement officers guarding the building. He thanked them for their service, posed for photographs with them and shook their hands.“He considers himself one of them and viscerally identifies with them,” Ms. Brown said. “Their sacrifices are not always recognized, and his decision to do so will be a huge part of his legacy.”Mr. Whitaker, a staunch social conservative, is likely to back many of Mr. Sessions’s policies.Mr. Sessions has no immediate plans for what he will do next, according to two of his associates. But in recent weeks, they said, people close to him have begun asking if he would consider running in 2020 for his old Senate seat in Alabama.“Jeff would have a good chance if he ran,” said George J. Terwilliger III, a former deputy attorney general and acting attorney general, who served under President George Bush. “Jeff remains quite popular and well regarded at home.”Indeed, Mr. Sessions has an additional natural constituency: law enforcement officials.“Law enforcement everywhere in Alabama greets him like a returning warrior,” Mr. Moore said.Career Justice Department lawyers who regarded Mr. Sessions with suspicion said they admired the decency with which he treated his staff.As dusk fell on Wednesday, more than 150 people gathered in the courtyard of the Justice Department to watch Mr. Sessions walk out for the last time. The crowd included scores of career employees, along with his deputy attorney general, Rod J. Rosenstein; the solicitor general, Noel J. Francisco; and Jody Hunt, Mr. Sessions’s former chief of staff who now runs the Justice Department’s civil division.Mr. Whitaker also saw him off. Soon after the two men shook hands, Mr. Sessions climbed into a black SUV and drove away.