Is forming a company outside my hometown legal?

It is most definitely legal. In fact, large corporations have done so for many years.