If you could take a really close look at a spider, then you would see that their feet are covered in tiny little triangular hairs. They look a little bit like paddles on the ends of stalks, and they give the spider a much bigger surface area. When the feet make contact with a wall or ceiling, they create a force – a temporary attraction between the bottom of the spider's foot and whatever surface it's on (the grown-up name for it is van der Waals forces).
So, how do they move without getting stuck? They can change the angle at which these little hairs are stuck to the wall. It's a bit like a Post-it note; tug at it from the middle and it won't budge, but you can peel it from one corner very easily. Spiders are actually very vulnerable. If a tarantula fell from a height, it would squish, because all its organs are in its abdomen and it doesn't have a skeleton to protect them. So they have to be very good at holding on.