Protector of the Realm is a title in the Seven Kingdoms, the holder of which is considered to be head of state. The title is usually held, ex officio, by the King of the Andals and the First Men, who also holds the title of Lord of the Seven Kingdoms. During the minority of a child monarch, however, a Regent is named as Protector of the Realm until the heir comes of age.
After King Robert is mortally wounded by a boar while hunting, he dictates his final orders to Hand of the King Eddard Stark, naming Lord Stark as regent and Protector of the Realm until his heir comes of age. Renly Baratheon correctly assumes that the dying Robert has given orders to this effect, as does Petyr Baelish. After Robert dies, Eddard arrives at court to find Joffrey Baratheon seated on the Iron Throne and introduced as both the new king and Protector of the Realm. However, Eddard has Ser Barristan Selmy read King Robert's final order before the royal court, naming Lord Stark as Protector of the Realm. Eddard proclaims that Joffrey has no claim to the throne, and orders the City Watch under Janos Slynt to take Joffrey and Cersei into custody. This sparks a standoff between Stark's personal guards and the Kingsguard, as Joffrey demands that Stark and all of his men be killed. Just then, Baelish and the Goldcloaks betray Eddard, killing all of his men and taking him prisoner. Joffrey then continues to technically hold the title of Protector of the Realm, though his mother Cersei still acts as Queen Regent.
In the books
In the A Song of Ice and Fire novels, the title of Protector of the Realm applies to the military command of the armies loyal to the Iron Throne, and is usually held by the King himself. Presumably, the holder of the office commands the four Wardens, who in turn are charged with commanding military forces in the four different cardinal directions of Westeros (North, South, East, and West).
In some instances, however, the title was granted to the Hand of the King or to a Prince Regent when the King was incapable or inexperienced in matters of war, or was otherwise incapacitated. Such was the case of King Daeron the Good, who named his own son and Hand of the King, Prince Baelor "Breakspear", as Protector of the Realm during and after the First Blackfyre Rebellion. Similarly, during the Dance of the Dragons Prince Daemon Targaryen, who commanded the armies of claimant Princess Rhaenyra Targaryen, held the title of Protector of the Realm. Meanwhile, on the opposing side of the war led by Rhaenyra's half-brother King Aegon II Targaryen, Aegon's II's younger brother Prince Aemond Targaryen was named Prince Regent and Protector of the Realm, after Aegon II was incapacitated after suffering grievous wounds in battle.