This story can be found in the Prose Edda.
Following the War between the Ęsir and Vanir the gods realized they needed to rebuild the wall that had surrounded Asgard, as it had been destroyed during the war. While the gods were talented and well skilled in building magnificent halls and buildings, they thought the task of building a fortress like wall beyond them. One day a tall man came across Bifrost and told Heimdall that he had a plan to bring before the gods. Odhinn gathered all the god and godesses to meet with the stranger and hear his plan. The stranger (who was the giant Hrimthurs in disguise) said he could rebuild the wall surrounding Asgard in 18 months. For payment he would take the sun and moon and also take Freya as his wife. Odhinn became angry and said the gods would never give up Freya, nor would they give up the sun and moon. He angrily bid the mason leave.
However, Loki begged the gods not to be so hasty and he asked the mason for some time to consider his offer. The mason left while all the gods and godesses gathered around Loki . Loki suggested they get the mason to agree that if he could build the wall in six months, starting on the winter solstice and completing the wall by the summer solstice then he would get the prize he desired. Loki explained there was no way the mason would be able to complete the task in the time alotted, but he would get a portion of the work done, thereby making it easier for the gods to finish the task and the gods wouldn't have to pay the mason at all.
Odhinn called the mason back into the hall and put Loki's plan before him. The mason seemed hesitant, but said he would agree to the terms if he was allowed to use his horse, Svadilfari to help him. The gods agreed and the bargain was struck.
The mason quickly set to work and was making amazing progress on the construction. The mason cut huge blocks of stone and the horse hauled loads heavier than anything the gods had seen. Throughout the winter the wall began to take shape. As winter began to fade and spring was almost upon them the gods realized the wall would be completed and they would have to give up Freya along with the sun and moon. The gods shouted angrily at Loki as it was his idea that led to this. They demanded he use his cunning to insure that the wall would not be completed on time.
Three nights before the solsitce Svadilfari was hauling the last of the stones toward the wall when the stallion spied a beautiful mare. He broke free of his harness and began chasing the mare. The mason pursued Svadilfari but could not catch him. The mason chased his stallion all night but could not catch him. The next day not as much work was done as had been done before. When the mason realized he would not be able to complete his work on time, stormed into Asgard shouting and raving. Dawn soon came and with it, summer. The mason had failed to complete the task. Over the course of the winter the gods had realized the mason was a giant. When the giant flew into a rage and threatened to destroy everything in Asgard Thor struck him a single blow with Mjollnir thus the giant died.
Several months later Loki returned to Asgard leading a young colt that had eight legs. The colt was the offspring of Svadilfari and Loki, who had disguised himself as the mare. Odhinn took the colt for his own and named him Sleipnir.