Polarimetric imaging mode of VLT/SPHERE/IRDIS. II. Characterization and correction of instrumental polarization effects