The two major causes of firing out of battery are hammer follow or a malfunctioning disconnector.
On external hammer guns like the 1911, if the hammer follows the slide it may strike the firing pin at just the right time so as to permit the pin to strike the live round, but the gun is not yet fully in battery (i.e. the slide is still partway back).
If the disconnector fails on any gun, the sear is effectively disabled during the recoil cycle, the lock no longer functions, meaning the hammer may follow (as above), or, on striker fired guns, the striker goes right forward as soon as at is done being pushed back. Same result.
This should never happen on any properly functioning handgun. Whereas, there are accounts of Glocks KBing even with factory ammo.
Many Glock fans deny this, but why then did they suddenly change the design, after seemingly being content for years telling owners not to reload for their Glocks? Either way, buy a new Glock and it shouldn't be susceptible to KBs with its new fully-supported chamber.
Also, some long guns, like later SKSs, have a "free floating" (i.e. half-assed engineering) firing pins which lack a firing pin return spring, which, in combination with "soft" non-mil-surp primers and some crud in the firing pin channel causing the pin to get stuck, leads to frequent out of battery discharges, or at least slam-fires.
Opinion: any gun with a firing pin which "normally" strikes the primer during feeding, and relies wholly on the hardness of the primer to avoid discharge from this, is of incomplete mechanical design.
Edit: with that said, it's a very good idea to keep your firing pin channels clean, as this can cause out of battery firing, or "full-auto" firing, on just about any gun. However, on most guns that represents an extreme case, and is rather rare