How can a country possibly prevent these things while still maintaining a free society?
You can't even prevent them when not being a free society. Its not like terrorism only occurs in free societies.
Terrorism doesn't need weaponry. The only deterrent would be to read people's minds, and you've probably watched Minority Report and other such dystopian scenarios. It's something that needs to be solved at the root, and TBF I don't believe it can be fixed.
This is not a Breivik, or a "Shoe Bomber" Reid; this is '70s-style, organised, cross-border terrorism -- the sort of which "we" were supposed to be good at handling by now.
Terrorism doesn't need weaponry.
Indeed. Even if you deem well-coordinated attacks with just edged weapons (like the fictional ones in "The Following") unlikely, note that the Multiple-Victim public homicide with the most fatalities in US history used not a single firearm.(and I'm not talking about 9/11, although it could count as such as well)