Mr Trump reversed his own policy amid international fury over the separation of undocumented parents and children.
He said he had been swayed by images of children who have been taken from parents while they are jailed and prosecuted for illegal border-crossing.
It was not immediately clear when Mr Trump's order would be implemented.
"It's about keeping families together," Mr Trump said at the signing ceremony on Wednesday.
"I did not like the sight of families being separated," he said, but added the administration would continue its "zero tolerance policy" of criminally prosecuting anyone who crosses the border illegally.
The executive order states that immigrant families will be detained together, except in cases where there are concerns about the child's welfare, but it is unclear for how long.
Mr Trump's order also calls for prioritising immigration cases involving detained families.
The president said his wife, Melania, and daughter, Ivanka, who reportedly have been applying pressure on him to drop the policy in recent days, "feel strongly" about ending the practice of separating migrant families.
"I think anybody with a heart would feel very strongly about it," he said. "We don't like to see families separated."
Vice-President Mike Pence and Department of Homeland Security Secretary Kirstjen Nielsen, who has emerged as the face of the White House policy, were both present for Wednesday's order signing.