President Obama on Tuesday afternoon touted the progress the U.S. has made in the fight against terror, warned that the threat will endure and appeared to launch a thinly veiled attack at President-elect Donald Trump.

Speaking to U.S. service members at MacDill Air Force Base in Tampa, Florida, Mr. Obama outlined how his core convictions on national security guided his policies in Iraq and Afghanistan and the fights against al Qaeda and the Islamic State in Iraq and Syria (ISIS).

In his final national security address, the president said that the U.S. is and must remain “the strongest fighting force the world has ever known” and the U.S. must not hesitate to act when necessary, including unilaterally. He explained why he insists that it’s unsustainable for the U.S. military to engage in nation-building overseas. Instead the U.S. should ask its allies to do their share in the counterterrorism fight, he argued.

Get Breaking News Delivered to Your Inbox

“Today, by any measure, core al Qaeda, the organization that hit us on 9/11, is a shadow of its former self,” Mr. Obama said, adding that plots directed from Afghanistan and Pakistan have been disrupted, its leadership has been decimated and Osama bin Laden is dead. “No foreign terrorist organization has successfully planned and executed an attack on our homeland -- and it’s not because they didn’t try.”

Mr. Obama defended his decision to withdraw U.S. troops from Iraq in 2011 and explained that while people like Mr. Trump argued it led to the rise of ISIS, the U.S. didn’t have an option at the time to stay.

“Iraqis wanted our presence there to end,” he said. But maintaining U.S. forces in Iraq would not have erased the forces that led to ISIS’s rise, and U.S. troops would have lacked legal protections had they stayed, he added.

Over the last two years, Mr. Obama said the U.S. has hit ISIS with more than 16,000 air strikes, and asserted that it has lost more than half of its territory and its control of major population centers. Its recruitment efforts are drying up and local populations are turning against the group, Mr. Obama said.

“We are breaking the back of [ISIS], we are taking away its safe havens and we’ve accomplished this at a cost of $10 billion over two years,” he said, which was the amount the U.S. used to spend each month at the peak of the Iraq war.

The president warned that “a deadly threat persists” and “some form of violent extremism will be with us for years to come.”

Mr. Obama laid out key points of advice that the next administration should consider in fighting the terror threat, which includes not elevating ISIS to make it seem like it poses an existential threat and upholding American values and the rule of law.

“The whole objective of these terrorists is to scare us into changing the nature of who we are and our democracy,” he said. “The fact is people and nations do not make good decisions when they are driven by fear.”

He said, for example, that the Obama administration prohibited torture everywhere including the use of waterboarding, which Mr. Trump has said he would want to bring back. Mr. Obama said that not using that tactic never cost the U.S. good intelligence.

The president also talked about how the U.S. cut the detainee population at the prison at Guantanamo Bay from 242 to 59, and he urged Congress to shut down the facility and transfer remaining detainees to prison in the U.S. He also discussed how the U.S. reformed its intelligence-gathering operations after Edward Snowden’s leaks about the National Security Agency (NSA).

He then continued to take jabs at Trump and subtly warned that the president-elect should uphold the Constitution.

“The United States of America is not a country that imposes religious tests as a price for freedom,” Mr. Obama said, adding that it’s also not a place where some citizens have to withstand greater scrutiny where they carry around an ID card.

Trump has talked about “extreme vetting” in his administration and proposed nearly a year ago, a ban on Muslims entering the U.S. He has since modified that plan so that it would block people from countries that are compromised by terrorism.

“We are a nation that has been defined by hope and not fear,” he said.