It depends on the company. My last two companies did it differently. One did the background check after inviting me to the interview but before the interview occurred, and my last company did it after they extended the job offer.
I know exactly when they did it because there's an issue that comes up in my background checks related to a guy with a similar name, birthdate, and city.