To understand the origin of the cycling performance improvement observed in lithium-sulfur (Li–S) batteries based on N-doped carbon materials, the interactions between lithium polysulfides (LiPSs) and N-doped graphene (N-G) with different doping configurations have been investigated by density functional theory calculations. It has been found that only N-G with clustered pyridinic N-dopants can strongly attract LiPSs with large enough binding energies to effectively anchor the soluble LiPSs, due to (i) an enhanced attraction between Li ions in LiPSs and pyridinic N-dopants and/or (ii) an additional attraction between S anions in LiPSs and Li ions captured by pyridinic N-dopants. This study has, for the first time, provided a fundamental understanding on the origin of the effective anchoring of LiPSs by N-doped carbon materials, which suppresses the shuttling of LiPSs and produces significant improvement in the cycling performance of Li–S batteries. These findings can also guide the design of more effective N-doped carbons or other N-rich materials for Li–S batteries, preventing the undesirable LiPS shuttling.