The gun has 2 rails, and a massive current (supplied by discharging a capacitor bank) flows from one rail, through the conductive projectile, and returns through the other rail. This generates a huge magnetic force which tries to push the rails apart, but as they are constrained, this instead pushes the projectile out the end.
In theory this is all that happens, in practise, the resistive heating from the huge current melts some of the projectile and the rails each shot. The "explosion" is these metal fragments igniting and burning in the surrounding air. A practical railgun takes a whole lot of maintenance!