In Japan it is considered a great honor to be invited to someone else’s home. This tradition has been passed down from generation to generation and it is just the way they to do it.
Beyond that it is for practical reasons. Japanese people have developed a custom of sitting and eating meals on tatami mats, not in chairs like in western cultures.
They also sleep on futons which you roll out onto a tatami mat.
Therefore they do not want to get their floors stained with dust, dirt, or soil that can be attached to the shoes. Instead the shoes are taken off in the genkan (玄関) and often there are slippers called uwabaki (上履き) to replace them with. Genkan are even found in small apartments.
Just wearing socks is also considered to be acceptable in informal situations. Socks, however, are not generally removed. Bare feet are usually okay when visiting close friends but not otherwise.
As always, if you have any questions please feel free to contact me at firstname.lastname@example.org or leave a comment below!