The battle for Castle Black was a battle fought between the King Beyond the Wall, Mance Rayder, and the Night's Watch. It took place during the winding down of the War of the Five Kings, but was not formally a part of that conflict.
Mance Rayder had once been a brother of the Night's Watch, until he abandoned his post and deserted for Beyond the Wall. He spent years uniting all of the tribes, a cause aided by rumors from the north of an ancient and terrible evil that had returned to the realms of men. Mance convinced the Freefolk that their only hope of survival was to make it behind the Wall - and to do that, they first had to take on the Night's Watch.
Jon Snow, having returned from Beyond the Wall, knows of Mance Rayder's approach, and begs Lord Commander Stannis Baratheon to beseech Robb Stark, the King in the North, for aid. Stannis agrees reluctantly, and pens a letter to King Robb Stark. Having only just recently defeated the Ironborn along his coasts, Robb Stark was on his way to Winterfell when the summons arrived. He left his foot behind, taking his horse to race toward Castle Black as quickly as possible.
After a test assault the first night, Mance sends giants and mammoths come dawn, hoping to pierce the Wall's defenses. With great loss they manage to succeed, defeating the invaders and holding the Wall. Over the next several days, Jon manages to throw back repeated attacks on the Wall by using catapults, archers, flaming arrows, boiling oils and finally frozen barrels of rock and ice used to destroy Mance's armored turtle.
Finally Robb Stark arrives, his men bringing reinforcements and supplies to the Watchers on the Wall. After meeting with Stannis, Robb agrees to take his men beyond the Wall, and sweep down upon the Wildlings during their next assault. The plan works perfectly, and the wildlings are largely put to rout, though Ramsay Snow and a few other lords are killed. Mance is captured, and thousands of prisoners are taken. These are forced to bury the bodies - a hard task, in the frozen ground - before being released back to their homes. Mance Rayder is executed by Lord Commander Stannis Baratheon, though not before he warns the Stag of what is to come.
With the threat on Castle Black thwarted, and the King Beyond the Wall dead, the crisis on the Wall is over. Stannis, however, uneasily remembers what Mance said before he died. The Lord Commander decides to find out if the rumors are true, and he takes with him nearly all the newly sworn Black Brothers that came with him from the south. They are never seen again, though the Nights Watch and the Wildlings both have tales of their fates.