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.
Removing shoes before entering a house is usually a cultural rather than a religious tradition. It is common to many Eastern countries, as well as Scandinavia. However, some religions require removing shoes before entering a house of worship or a temple. Muslims remove their shoes before entering a mosque, Hindus remove shoes before entering a temple, and Sikhs remove shoes before entering a gurudwara.
Because in ancient time the entire house was a bed. Why do you sleep on beds? To avoid sleeping directly on the floor, which is dirty, humid, crawling with all kinds of insects. But in ancient times in Japan the houses were built over a platform which protected the house from dirt, humidity and insects. The entire house was a bed, so to speak. That is why to this day Japanese people remove their shoes…