The conversion of 5-Hydroxypentanal to 2-methoxytetrahydropyran takes place in two steps. In first step the carbonyl group of 5-hydroxypentanal gets protonated and is being attacked by the hydroxyl group of its own molecule and results in the formation of six membered hetrocyclic ring called tetrahydro-2H-pyran-2-ol.
The tetrahydro-2H-pyran-2-ol on protonation looses the water molecule results in the formation of second intermediate called as 2,3,4,5-tetrahydropyrylium (Shown in red). This intermediate when attacked by Methanol molecule results in the formation of 2-methoxytetrahydropyran as shown below.