One must have put his socks (or whatever covering he is using) while in a state of purity.
Said al-Mughirah ibn Shu’bah, “I was with the Messenger of Allah, upon whom be peace, one night during an expedition. I poured water for him to make ablution. He washed his face and arms and wiped his head. Then I went to remove his socks and he said, ‘Leave them on, as I put them on while I was in a state of purity,’ and he just wiped over them.” (Related by Ahmad, al-Bukhari and Muslim).
Al-Humaidi related in his Musnad that al-Mughirah reported, “We said, ‘O Messenger of Allah, may we wipe over our socks?’ He said, ‘Yes, if you put them on while you were in a state of purity.” The stipulations by the jurists that the socks must completely cover the foot to the ankle, and that one must be able to walk (a distance) in them alone, has been shown by Ibn Taimiyyah in his al-Fatawa to be weak.