Physics of planet trapping with applications to HL Tau