The Islamic State militancy has lost most of the territory it had in Syria and Iraq, but it remains potent as inspiration for terrorists-in-waiting.