KABUL—In Wednesday's early hours, President Barack Obama delivered a simple message during a surprise visit to America's largest military base in Afghanistan: The U.S. will not allow this country to once again be overrun by Islamic hard-liners.

Two hours later, as Mr. Obama was flying back to the U.S., Taliban insurgents issued a jarring reminder of the challenges America still faces in trying to bring a dignified end to a decade of war in Afghanistan.

At...