Well, of course, the U.S. lost more people in one terrorist attack in a single day than any country has lost in decades.
And those guys got in here legally via visas but stayed here on expired visas. And, you know how big this country is and hard it is to locate people with expired visas (needle in the world's biggest haystack). There were all kinds of "signs" pointing to these guys doing some stupid shit, but hindsight is always 20/20. So they got through airport security, used jumbo jets fully-loaded with fuel as giant bombs, and killed 2,996 people and injured over 6,000 people.
So, yes, how do we "avoid" this happening again?
Answer: We can't.
Yes, we should monitor EVERYBODY who comes into this country so we can keep track of visas; Polish, Greek, German, whomever. But, this would require a huge budget, and we're barely able to keep up with undocumented immigrants, already. And, we have had terrorist acts committed by American Citizens.
Better security is the best answer, which is how we have been combating this problem so far. Our American post-9/11 reality is INSANE levels of security everywhere. We get frisked and wanded and our bags are checked at frigging RAVINIA, now, which is the summer home of the Chicago Symphony Orchestra. We aren't allowed to bring even a purse to a Chicago Bears game, anymore (except about the size of a deck of cards). There are cops, barricades, guys with guns at pretty much all festivals (post-Boston Bombers).
AND, even with ALL THAT, we STILL can't avoid assholes
LIKE THIS.