8,000 years ago, during the period when the Children of the Forest and the First Men lived in peace together, The Long Night came. The Long Night is regarded as the longest winter ever and it brought total darkness for an entire generation. It was during this time that the White Walkers raised an army of the dead and first invaded from the very North of Westeros. The First Men and the Children of the Forest banded together to fight the White Walkers. During the Long Night, many died and both the First Men and the Children were pushed near the brink of extinction. It was at this time that The First Men and the Children of the Forest discovered that blades made from Dragonglass could kill the White Walkers, which helped to win the war and push back the White Walkers.
The Long Night came to an end after the Battle of the Dawn. According to the eastern religion of The Lord of Light, the battle was led by Azor Ahai. He fought with a great sword of fire, called Lightbringer. With this sword, Azor Ahai pushed back the White Walkers and died doing so. After his death, it was prophesized that Azor Ahai, who is also known as the Prince tho was Promised, would be reincarnated to once again lead the battle against the White Walkers at some point in the future. The prophecy notes that if he failed, all of civilization would fail along with him and cease to exist. Interesting to note, that Melisandre, the Red Priestess who follows the Lord of Light, refers to Stannis Barahteon as The Prince Who Was Promised, and he too wields a sword called Lightbringer. It appears as though she believes he is the reincarnate of Azor Ahai who will once again lead the fight against the White Walkers.
After defeating the White Walkers, it was at this time that the First Men built the Wall to protect all future civilizations from the evils north of the Wall. The person credited with building the wall is Brandon Stark, otherwise known as Brandon the Builder. He is generally referred to as the first Stark and founder of House Stark. Along with the help of giants and the magic of the Children of the Forest, he built the wall to stretch across the entire continent of Westeros at its northern most point so that Westeros was completely protected from the White Walkers. After the wall was built, the Night’s Watch was formed to protect and defend the Wall from all that lived north of it.