A veteran of the no-holds-barred world of New York tabloids that Mr. Trump also inhabited for decades, Mr. Giuliani was an early and vocal supporter of Mr. Trump’s presidential campaign. He was also a forceful critic of Hillary Clinton, a longtime political rival and Mr. Trump’s opponent in the 2016 presidential race.

For Mr. Giuliani, the embrace of Mr. Trump’s candidacy at times appeared to be a likely entree back into the limelight of national politics after a disastrous 2008 bid for the White House. An energetic speech he delivered in 2016 at the Republican National Convention, at which Mr. Trump was nominated, prompted talk of Mr. Giuliani’s being appointed secretary of state or, perhaps, of his leading the Department of Homeland Security.

Those aspirations never materialized. Mr. Giuliani was passed over for both jobs — not once, but twice for each position.

Still, Mr. Giuliani has never wavered in his support for his fellow New Yorker. And when Mr. Trump was seeking a new lawyer — a high-wattage TV combatant who would not wilt under the pressures of the news media or legal adversaries — Mr. Giuliani seemed a natural pick.