US Judge Ann M. Donnelly held that the petitioners had a "strong likelihood of success" in establishing that their removal "violates their rights to Due Process and Equal Protection guaranteed by the United States Constitution."
Donnelly, who was appointed to the bench by President Barack Obama, sits on the US District Court for the Eastern District of New York.
A White House spokesperson defended the order Sunday, saying: "It is the right and duty of the President to do everything in his legal and constitutional power to protect the American people."
"Saturday's ruling does not undercut the President's executive order. All stopped visas will remain stopped. All halted admissions will remain halted. All restricted travel will remain prohibited. The executive order is a vital action toward strengthening America's borders, and therefore sovereignty. The order remains in place," the spokesperson said.
The court's ruling came after immigration lawyers at the Americans Civil Liberties Union and other groups flocked to airports across the country to come to the aide of individuals who had arrived with valid immigrant visas and found themselves in legal limbo. The lawyers asked for a nationwide stay that would block the deportation of all people stranded in US airports under what the group called "President Trump's new Muslim ban."
Donnelly granted their request, writing: "There is imminent danger that, absent the stay of removal, there will be substantial and irreparable injury to refugees, visa-holders, and other individuals from nations subject to the January 27, 2017, Executive Order."
Trump's order, titled "Protection Of The Nation From Foreign Terrorist Entry Into The United States," makes good on his longtime campaign promise to tighten borders and halt certain refugees from entering the United States.
As well as the ban on travel from seven named countries for 90 days, it also caps the total number of refugees admitted into the United States during the 2017 fiscal year at 50,000, down more than half from the current level of 110,000. It halts all refugees from Syria indefinitely.
"I am establishing new vetting measures to keep radical Islamic terrorists out of the United States of America," Trump said during the signing at the Pentagon. "We don't want them here."
As Donnelly was considering the case, lawyers around the country rushed to file legal briefs.
Early Sunday morning, a federal court in Massachusetts also issued a temporary restraining order, blocking part of the executive order in a case brought by lawyers for two lawful permanent residents who are college professors. That order, issued by United States District Court Judge Allison D. Burroughs, also an Obama appointee, went a step further ordering that the government could not "detain or remove" individuals who had arrived legally from the countries subject to Trump's order: Iraq, Syria, Iran, Sudan, Libya, Somalia and Yemen.
Burroughs said that her order applied to the petitioners in the case as well as those similarly situated including lawful permanent residents, citizens, visa-holders, approved refugees, and other individuals from nations who are subject to the January 27, 2017, executive order. The order was also signed by United States Magistrate Judge Gail Dein. The judges specified that the order would remain in effect for seven days while the court scheduled a hearing.
The United States denied entry to 109 travelers heading to the country at the time the ruling was signed, a Department of Homeland Security official said. The agency would not say how many of the 109 were sent already home and how many were detained.
The ruling does not necessarily mean the people being held at airports across the US are going to be released, said Zachary Manfredi, from Yale's Worker and Immigrant Rights Advocacy Clinic, who helped draft the emergency stay motion.
"The judge's order is that they (lawful visa/green card holders) not be removed from the US -- it doesn't immediately order that they be released from detention," he told CNN.
"We are hoping that (Customs and Border Patrol), now that they no longer have a reason to detain them, will release them. But it is also possible they could be transferred to (other) detention facilities," Manfredi said.
"We are getting the order to as many (Customs and Border Patrol) officers as possible right now," he added.
ACLU officials praised the judge's ruling.
"Clearly the judge understood the possibility for irreparable harm to hundreds of immigrants and lawful visitors to this country," said ACLU executive director Anthony D. Romero. "Our courts today worked as they should as bulwarks against government abuse or unconstitutional policies and orders. On week one, Donald Trump suffered his first loss in court."
Lee Gelernt, deputy director of the ACLU's Immigrants' Rights Project, who argued the case, said the ruling "preserves the status quo and ensures that people who have been granted permission to be in this country are not illegally removed off US soil."
Lawsuit is first challenge to executive order
The class-action lawsuit in New York is the first legal challenge to Trump's controversial executive order, which indefinitely suspends admissions for Syrian refugees and limits the flow of other refugees into the United States by instituting what the President has called "extreme vetting" of immigrants.
The two Iraqis, Hameed Khalid Darweesh and Haider Sameer Abdulkaleq Alshawi, had been released by Saturday night. But lawyers for other detained travelers said in a court filing that "dozens and dozens" of individuals remained held at JFK.
Similar legal actions were initiated in other states before the ruling in New York took effect.
A federal court in Washington state issued a stay forbidding travelers being detained there from being sent back to their home country.
A federal court in Virginia has issued a temporary restraining order saying several dozen permanent residents returning from trips abroad should have access to lawyers while they are being detained at Dulles International Airport and these residents cannot be removed from the United States for seven days.
'America is the land of freedom'
According to court papers, both Darweesh and Alshawi were legally allowed to come into the US but were detained in accordance with Trump's order.
Darweesh, who worked as an interpreter for the US during the Iraq War, was released from detention early Saturday afternoon.
"America is the land of freedom," he told reporters at the airport shortly after his release. "America is the greatest nation."
A source with knowledge of the case confirmed Darweesh will be allowed into the US due to provisions in Trump's order that allow the State and Homeland Security departments to admit individuals into the US on a case-by-case base for certain reasons, including when the person is already in transit and it would cause undue hardship and would not pose a threat to the security of the US.
The suit said Darweesh held a special immigrant visa, which he was granted the day of Trump's inauguration on January 20, due to his work for the US government from 2003 to 2013.
Alshawi was released Saturday night, according to his attorney, Mark Doss.
Rep. Nydia Velazquez, D-New York, who had arrived at JFK by early Saturday afternoon to try and secure the release of the two Iraqis, railed against Trump's order and pledged continued action.
"This should not happen in America," Velazquez said following Darweesh's release. "One-by-one, street-by-street, if we have to go to court, we will fight this anyplace, anywhere."
'The executive order is unlawful'
The lawsuit said the US granted Alshawi a visa earlier this month to meet with his wife and son, whom the US already granted refugee status for her association with the US military.
The lawyers for the two men called for a hearing because they maintain the detention of people with valid visas is illegal.
"Because the executive order is unlawful as applied to petitioners, their continued detention based solely on the executive order violates their Fifth Amendment procedural and substantive due process rights," the lawyers argue in court papers.
Court papers said Customs and Border Patrol authorities did not allow the lawyers to meet with the men, and told them to try reaching Trump. Velázquez and fellow New York Democratic Rep. Jerrold Nadler said they attempted to speak to Darweesh and Alshawi at JFK's Terminal 4 earlier Saturday but were denied.
"When Mr. Darweesh's attorneys approached CBP requesting to speak with Mr. Darweesh, CBP indicated that they were not the ones to talk to about seeing their client. When the attorneys asked, 'Who is the person to talk to?' the CBP agents responded, 'Mr. President. Call Mr. Trump,' " the court papers read.
Doss, an attorney with the International Refugee Assistance Project, told CNN his clients knew they had to get to the US as soon as possible so they boarded the first flight they could.
The two men had been allowed to make phone calls. They do not know each other, and it is unclear if they were held together or separately, or if they were kept in a holding cell, according to Doss.
The lawsuit was earlier reported
by The New York Times.
But the order immediately raised questions about its legal standing.
"What this order does is take people who played by the rules who have been rigorously vetted and slams the door of the country in their face because of their religion," said David Leopold, an immigration attorney and the former president of the American Immigration Lawyers Association.
But Stephen Yale-Loehr, a professor of immigration law practice at Cornell Law School, said Trump's order may have merit.
"The plaintiffs are alleging a variety of reasons why they should not be detained under the executive order. Their arguments include the equal protection clause of the Constitution and a provision in the immigration law that prohibits national origin discrimination," Yale-Loehr said. "However, presidents have wide discretion on immigration, because immigration touches national sovereignty and foreign relations. Courts tend to defer to whatever a president declares on immigration. I think the administration could win."
An administration official told CNN if a person has a valid visa to enter the US but is a citizen of one of the seven countries under the temporary travel ban, then the person cannot come into the US. If the person landed after the order was signed Friday afternoon, then the person would be detained and put back on a flight to their country of citizenship.
Separately, Department of Homeland Security officials acknowledged people who were in the air would be detained upon arrival and put back on a plane to their home country. An official was not able to provide numbers of how many have already been detained.
Democrats and liberal-leaning groups continued to denounce Trump's order Saturday as racist.
"The first two refugees kept out of America by President Trump's order are Iraqis who helped US troops survive during the war," Rep. Jim Cooper, D-Tennessee, said on Twitter. "These men risked their lives for years to keep our troops safe. One was with the 101st Airborne. This is no way to treat our allies."
Vermont Sen. Bernie Sanders called the action an "anti-Muslim order" that "plays into the hands of fanatics wishing to harm America."
And Senate Minority Leader Chuck Schumer called on the Trump administration to "rescind these anti-American executive actions that will do absolutely nothing to improve our safety."
Travelers turned away
Sarah Assali from Allentown, Pennsylvania, told CNN Saturday that the US government stopped six of her Syrian family members at Philadelphia International Airport and sent them back to Doha, Qatar, the origin city of their flight.
Assali said her family members were persecuted Christians and did not speak very good English, so they did not fully understand why they were being put on a plane back to the Middle East.
Additionally, some US-bound travelers who were either refugees or from countries specified in Trump's executive ban have been turned back from Cairo International Airport, according to a Cairo airport official who is not authorized to speak to the media.
The official said they were required to follow the instructions of the United States in the matter. He said "(The airport personnel) have no choice but to follow orders. I don't want to blame anyone here."
CNN's Kevin Bohn, Ryan Nobels, Ellie Kaufman, Patricia Pedraza, Sheena Jones, Mohammed Tawfeeq, Rachel Crane, Chris Welch and Sarah Jorgensen contributed to this report.