Stack Exchange actually allows you to run queries against its data.
From there you can get the SE data model. As you can see
Badges have its own table. My guess would be that for any action that happens, if that action could trigger someone to earn a badge, it is checked for right then and there, at that instant. If a badge is earned record is logged into the badges table.
I assume everything is logged as well so the badges could be calculated on the fly, but that would be way too slow. Hence the de-normalized table structure. I also assume thay have some process that could recalculate a user's badges the same way they have a system in place to recalculate reputation. (Reputation is also de-normalized for performance reasons.)
But I am just guessing. I have no way of of knowing exactly how the SE code base works.