Methylation of toluene with methanol to produce p-xylene has been investigated for decades, but the origin of selectivity is still under debate. Here we report computational studies based on ab initio molecular dynamics simulations and free energy sampling methods to identify the key steps determining the selectivity. The steps of toluene methylation to protonated-xylene, deprotonation of protonated-xylenes, and diffusion of xylene in HZSM-5 channels are compared. We find the pathways of formation for protonated p-/m-xylenes have similar free energy barriers. Meanwhile, the methylation is found rate-determining, thus the probability to generate p-/m-xylenes at the active site are similar. We then find that the diffusion for m-xylene along the zigzag channel is more difficult than its isomerization to p-xylene, which in turn further promotes the selectivity of p-xylene formation. These insights obtained at the molecular level are crucial for further development of high-performance zeolite catalysts for toluene methylation.