The reaction is a substitution reaction. Basically, the electrophile is the 5' alpha phosphate, the nucleophile is the 3' hydroxyl, and the leaving group is the pyrophosphate (the gamma and beta phosphates). As in most other substitution reactions, the nucleophile displaces the leaving group on the electrophile.
Why is this reaction favorable? Well, breaking the bond between the alpha and beta phosphates does not release energy. Rather, this bond is very weak so the energy released by forming a bond between the 3' OH and the 5' alpha phosphate is greater than the energy needed to break the alpha-beta phosphate bond. Furthermore, the pyrophosphate is very favorably hydrolyzed into two phosphates. This leaves a low steady state concentration of pyrophosphate which helps make the formation of phosphodiester bonds more favorable.