(CNN) -- President Obama rallied U.S. troops and pledged continued partnership with Afghanistan during a previously unannounced trip to the country Sunday.
Speaking to about 2,000 U.S. and allied troops at the major U.S. base in Afghanistan, Obama said, "Those folks back home are relying on you."
"I know it's not easy," he said. "You're far away from home. You miss your kids, you miss your spouses, your family, your friends." But he added, "If I thought for a minute that America's vital interests were not served, were not at stake here in Afghanistan, I would order all of you home right away."
Earlier, Obama met with Afghan President Hamid Karzai at the Presidential Palace to discuss progress by the Afghan government in strengthening its ability to run the country and provide security for its people.
After the 30-minute meeting, Obama said he wanted to send a strong message that the partnership between the nations would continue. Obama also invited Karzai to Washington, and White House Press Secretary Robert Gibbs said the planned date for that visit is May 12.
Karzai said he wanted to "express the gratitude of our people for the help that America has given us for the last eight years," and he specifically thanked U.S. taxpayers for their aid in helping rebuild his country.
However, Obama made clear that his main reason for the trip was to visit with some of the roughly 80,000 U.S. troops stationed in the country.
After changing from a suit to an Air Force One flight jacket, Obama told the troops they were making progress against al Qaeda and its allies in the Taliban, the Islamic fundamentalist militia that ruled most of Afghanistan before the 9/11 attacks.
"All of that makes America safer, and we are going to keep them on the run," he said. "Because that is what is going to be required in order to ensure that our families back home have the security that they need."
To cheers from the combined joint task force that includes troops from all four services, Obama said: "The United States of America does not quit once it starts on something. ... We keep at it. We persevere. And together, with our partners, we will prevail. I am absolutely confident of that."
Following his speech to the troops, Obama met with U.S. Ambassador Karl Eikenberry and Gen. Stanley McChrystal, the commander of allied troops in Afghanistan, before his scheduled departure for the flight home.
Obama's trip began in secrecy, with the president leaving his Camp David retreat. He flew to Afghanistan on Air Force One, landing at Bagram Air Base at 7:24 p.m. (around 11 a.m. ET). Obama then flew on a helicopter to the Presidential Palace to meet with Karzai.
The two presidents took part in a 10-minute arrival ceremony before walking into the palace for their meeting. U.S. officials said the two leaders planned to first meet along with key aides, and then bring in Karzai's cabinet.
In a briefing with pool reporters on the trip, National Security Adviser James Jones said Obama would push Karzai to improve efforts to meet benchmarks for better governance in his second term, which began last year.
According to Jones, Obama planned to point out that certain issues such as a merit-based system for appointing key government officials, battling corruption and taking on narcotics trafficking that helps fund insurgents needed more attention.
"We have to have the strategic rapport with President Karzai and his Cabinet to understand how we are going to succeed this year in reversing the momentum the Taliban and the opposition forces have been able to establish since 2006," Jones said.
A senior administration official said Obama told Karzai that some progress had been made, particularly involving local governance, establishing more credible national institutions and battling corruption.
The senior official characterized the meeting as very productive and businesslike.
The trip came after what has been perhaps the most successful week of Obama's presidency, in which he signed comprehensive health care legislation -- his top domestic priority -- into law and announced plans to sign a new nuclear arms reduction treaty with Russia on April 8.
Gibbs said Afghan authorities were notified of Obama's trip on Thursday. The president previously postponed a trip to Indonesia and Australia planned for March 21-26 in order to see through passage of the health care bill.
Since taking office, Obama ordered the deployment of an additional 30,000 troops to Afghanistan while also saying he intended for combat forces to start coming home in July 2011.
U.S. officials said Sunday there were nearly 80,000 U.S. troops now on the ground in Afghanistan, and the surge announced by Obama would increase the total to 98,000 once fully implemented by the end of the year. Roughly 10,000 of the 30,000 surge troops were in place now, officials said. In addition, NATO partners have more than 40,000 troops in Afghanistan, according to the officials.
Karzai has said that by the end of 2010, Afghan security forces will be able to take over some security responsibilities from international forces. By the end of his second term in 2014, Karzai has said he would like his government to have full responsibility for security.
Karzai emerged victorious from August presidential elections that were marred by widespread fraud. Two months after the vote, a U.N.-backed panel of election monitors threw out nearly a third of Karzai's votes, citing fraud. Left short of a majority, Karzai agreed to a runoff with challenger Abdullah Abdullah, but Abdullah withdrew just before the runoff vote.
The trip is Obama's second to Afghanistan. He first traveled there in 2008, four months before his victory in the election that made him president. On that visit, part of a broader trip to the Middle East, Obama traveled to eastern Afghanistan to meet with U.S. forces and also met with Karzai.