Why do the Japanese take their shoes off before entering a house?

Culturally speaking, because it's traditional and it is the polite thing to do.

Many cultures have similar customs. For instance, in Russia it is considered impolite to wear a hat indoors. The custom of removing shoes is widespread in other Eastern countries besides Japan, especially in Korea and Turkey. It is also present in Scandinavian countries. In Sweden it is a major faux pass to walk through a house with shoes on. Some schools in Sweden even require shoes to be removed.

As a practical matter, traditionally there was very little furniture in Japan. The floors in traditional Japanese dwellings are covered with tatami mats. These mats are used to sit on and to sleep on instead of chairs and beds. The streets and roads were muddy and dirty. Wearing your shoes into the house would bring the mud, dirt, dust and bacteria into the house and you would have to sleep in all that. It would be much better to leave the shoes and the dirt out. Also, the mats do not stand up to footwear very well, meaning that anything but bare feet or socks would rip them up.

Even though Western-style raised furniture and hard flooring is common in Japan now and the pavement technology is much improved, the tradition remains, in part because the Japanese like traditions and in part because it makes sense to have a cleaner house. Even good pavement has some dirt on it.