There are different stories behind Vaastupurush.
In 'Tretayug' there was a creature who obstructed the Earth and Sky with his huge body. All the mankind, demons and the Devtas were frightened by this. They went to Lord Bramha. Lord Bramha asked them to throw the creature with its face down.The Devtas caught that creature and threw it on Earth with its face down. The ceature fell down on Earth with his head in the North-East direction and feet in the South-West direction. The different parts of the body (of that creature) were held by 81 Devtas and the Devtas started residing on that part of the body. The creature begged for pardon to Lord Bramha. Lord Bramha gave him a boon that those who worship him as Vastudevta and give him Balibhoj as part of scrifice, he give them all the pleasure, happiness and prosperity and those who avoid Vastudevta while constructing buildings or any other structure, he will trouble them. Lord Brahma, had made that creature as God of the House or Vaastu Purusha.
There is another interesting story of Vaastu Purusha in Matsya Purana. It narrates the birth of Vastu Purusha. In an encounter with a Rakshasa called Andhikasur, Lord Shiva became tired and exhausted, and he began to sweat profusely. A man was born out of the drops of Lord Shivas sweat. He looked very cruel. He was very hungry and began to make penance to appease Lord Shiva and to get a boon from him. Lord Shiva was pleased with his penance and appeared before him. The devotee prayed to Lord Shiva, Oh Lord! Please permit me to eat away all the three worlds. Lord Shiva granted his prayer. He got possession of all the three worlds and was about to eat the terrestrial world. The celestial beings, Brahma, Shiva, and the demons were terrified and they caught hold of the devotee and encircled him. Forty-five deities caught hold of the devotee and pressed him down. This Devotee has been in that position ever since that time. Lord Brahma blessed him and said that he would be the deity of all the plots and constructions, and offerings would be made to him. In return Vastu Purusha is said to be taking care of the inmates of the building.