From Wikipedia entry of Spider-man's power -
The ability works through thin layers of cloth, such as the fabric of
his costume, but not through materials such as the soles of shoes.
When Peter Parker needs to crawl without changing into the costume, he
removes his shoes first.
Spider-Man's wall-crawling ability has increased with time, most
notably in a backup story in Amazing Spider-Man #365 by Tom DeFalco,
in which Peter explains his abilities to Mary Jane. Peter reveals that
every part of his body has gained or increased in attraction to other
surfaces, and he can stick people or objects on his back. Over time he
realizes he could keep his mask firmly fixed to his face (previously
used to prevent undesirable mask removal), protecting his secret
The Spider-Man entry in the Official Handbook of the Marvel Universe states that Spider-Man is able to enhance the flux of inter-atomic attractive forces on surfaces he touches, increasing the coefficient of friction between that surface and himself.
Electro had a revelation during a battle about Spider-Man's clinging ability and can disrupt this power somehow, claiming that it was based on some sort of electromagnetic bonding. It is ineffective after the events of "The Other".
So it can be concluded that they used Spider-Man's wall-crawling in the comics first and then they tried to explain it time by time. Same with the movies too.
Note: Answer is from the comic universe because movie don't have any elaborated answer for this.