It was a dramatic turnaround for Trump, who has been insisting, wrongly, that his administration had no choice but to separate families apprehended at the border because of federal law and a court decision.
The news in recent days has been dominated by searing images of children held in cages at border facilities, as well as audio recordings of young children crying for their parents — images that have sparked fury, question of morality and concern from Republicans about a negative impact on their races in November's midterm elections.
Until Wednesday, the president, Homeland Security Secretary Kirstjen Nielsen and other officials had repeatedly argued the only way to end the practice was for Congress to pass new legislation, while Democrats said Trump could do it with his signature alone. That's what he did on Wednesday.
"We're going to have strong, very strong borders, but we're going to keep the families together," said Trump who added that he didn't like the "sight" or "feeling" of children separated from their parents.
The order does not end the "zero-tolerance" policy that criminally prosecutes all adults caught crossing the border illegally. It keeps families together while they are in custody, expedites their cases, and asks the Department of Defense to help house them.
Justice Department lawyers have been working to find a legal workaround for a previous class-action settlement that set policies for the treatment and release of unaccompanied children who are caught at the border. Trump's new order states the attorney general will seek to modify the agreement, known as the Flores settlement, to allow Homeland Security to detain families together until criminal and removal proceedings are completed.
Trump's order is likely to create a fresh set of problems involving length of detention of families, and may spark a fresh court fight. The language also leaves room to separate children from parents if it's best for the child's welfare.
Assessments for possible detainee use have been done at three bases in Texas: Dyess Air Force Base, Goodfellow Air Force Base and Fort Bliss. An assessment is expected Thursday at Little Rock Air Force Base in Arkansas.
Also playing a role in the president's turnaround: First lady Melania Trump. One White House official said Mrs. Trump had been making her opinion known to the president for some time that she felt he needed to do all he could to help families stay together, whether by working with Congress or acting on his own.
Homeland Security Secretary Nielsen traveled to Capitol Hill Wednesday afternoon to brief lawmakers. And lawmakers on the fence over pending immigration legislation headed to the White House to meet with Trump.
Two people close to Nielsen said she was the driving force behind the turnabout that led to the new order keeping families together. Those people were not authorized to speak publicly and commented only on condition of anonymity.
One of them said Nielsen, who had become the face of the administration's policy, had little faith that Congress would act to fix the separation issue and felt compelled to act. She was heckled at a restaurant Tuesday evening and has faced protesters at her home.
Trump had tweeted early Wednesday, before issuing his order, "It's the Democrats fault, they won't give us the votes needed to pass good immigration legislation. They want open borders, which breeds horrible crime. Republicans want security. But I am working on something - it never ends!"
The "zero tolerance" policy put into place last month moves adults to the custody of the U.S. Marshals Service and sends many children to facilities run by the Department of Health and Human Services.
Under the Obama administration, such families were usually referred for civil deportation proceedings, not requiring separation. There currently are three family detention centers with a total 2,700-bed capacity.
The policy had led to a spike in family separations in recent weeks, with more than 2,300 minors separated from their families at the border from May 5 through June 9, according to Homeland Security.
The Flores settlement, named for a teenage girl who brought the case in the 1980s, requires the government to release children from custody and to their parents, adult relatives or other caretakers, in order of preference. If those options are exhausted, authorities must find the "least restrictive" setting for a child who arrived without parents.
In 2015, a federal judge in Los Angeles expanded the terms of the settlement, ruling that it applies to children who are caught with their parents as well as to those who come to the U.S. alone. Other recent rulings, upheld on appeal, affirm the children's rights to a bond hearing and require better conditions at the Border Patrol's short-term holding facilities.
In 2016, the 9th U.S. Circuit Court of Appeals ruled that child migrants who came to the border with parents and were held in custody must be released. The decision did not state parents must be released. Neither, though, did it require parents to be kept in detention, apart from their children.
Associated Press writers Zeke Miller, Eric Tucker, Ken Thomas and Alan Fram contributed to this report.