In her ruling on Thursday, Justice Saliann Scarpulla of State Supreme Court in Manhattan found that Mr. Trump had in fact “breached his fiduciary duty” by using the foundation to advance his business and political interests.

The suit was one of several legal battles that arose between Mr. Trump and state prosecutors around the country from the moment he took office, especially in New York. The initial investigation into the foundation was launched by the former attorney general, Eric T. Schneiderman, and continued through the subsequent administrations of two attorneys general. The lawsuit was also based on information first reported by The Washington Post.

As part of the settlement negotiations, the attorney general’s office had asked Justice Scarpulla to ban Mr. Trump from ever running a charity again. Though the judge did not impose such a ban, she did place numerous restrictions on Mr. Trump should he seek to establish another charitable organization.

The attorney general’s office had also asked Justice Scarpulla to award damages of $2.8 million — the amount that the foundation had raised at the Iowa fund-raiser. That request was turned down too.

The final details of the settlement were worked out between the current New York State attorney general, Letitia James, and Mr. Trump’s lawyers on Oct. 1.

Under its terms, Mr. Trump’s lawyers agreed to give the Trump Foundation’s remaining assets of $1.7 million to a group of charities that have no connection to the president or his family. They included the Children’s Aid Society, Citymeals on Wheels, the United Negro College Fund, the United Way of the National Capital Area and the U.S. Holocaust Memorial Museum. The $2 million in damages Justice Scarpulla awarded, which were akin to a fine, were to be paid to those same groups.

Both sides agreed that three of Mr. Trump’s children who were officers of the foundation — Donald Trump, Jr., Ivanka Trump and Eric Trump — will undergo training in order to ensure they do not engage in similar improprieties. As board members of the foundation, the president’s children were named as defendants in the lawsuit.