The Riot of King's Landing was an event during the War of the Five Kings. After seeing off Princess Myrcella Baratheon at the docks on a ship bound for Dorne, the royal procession including King Joffrey is subjected to the jeers of the starving crowds of the capital city, culminating in Joffrey foolishly ordering his outnumbered personal guard to execute the entire crowd, sparking off a city-wide riot.
After the outbreak of the War of the Five Kings, the problems in King's Landing multiplied, particularly the worsening food shortages. In normal times, food is mostly brought in to the capital city from the Reach and the Riverlands, because the Crownlands alone cannot feed its large population. However, the Riverlands could not send food, because House Tully had sided with Robb Stark's rebellion, and the southern portions of the Riverlands that Lannister armies nominally controlled were reduced to a burned-out, ravaged war zone. Meanwhile, House Tyrell of the Reach sided with King Renly Baratheon, cutting off food shipments to the capital along the Roseroad. Even though Renly recently died not long before the riot, House Tyrell was still wavering in which faction to subsequently realign itself with, and had not resumed food shipments.
Added to this, there was a large influx of refugees from the Riverlands into the city since the war began, seeking the protection of the city walls. This drastically exacerbates the food shortages. Lord Commander Janos Slynt of the City Watch had desperately tried to convince Queen Cersei that the dire situation was spiraling out of control, as did her brother Tyrion, but she waved aside such concerns.
As the royal procession worked their way back to the Red Keep, crowds of peasants gathered around them, and some began sarcastically shouting empty praises at Joffrey. Soon they began begging for bread because they are starving due to the disruptions in trade due to the war. After being subjected to the japes and jests of the crowd, including shouts of "bastard!", "monster of incest!", "Stannis!", and a sarcastic "Hail King Joffrey!" someone threw a cow pie at Joffrey's head.
Enraged at the excrement thrown in his face, Joffrey demanded that his guards seize whoever threw it. With mounting frustration, Joffrey became livid, and simply shouted out "Just kill them! Kill them all!"
Unfortunately for Joffrey, normally used absolute power in ordering the torment of Sansa Stark or defenseless peasants dragged into the throne room, his word actually wasn't law. Utterly failing to realize that he wasn't in the safety of the Red Keep with its large garrison, Joffrey's few dozen guards were quickly overwhelmed by hundreds of panicking and angry commoners and refugees. The High Septon was dragged down screaming by the crowd and torn limb from limb.
The Kingsguard carved a path through the mob with their swords, barely managing to rush Joffrey, Cersei, and Tyrion to the security of the Red Keep. Joffrey was left fuming, but Tyrion blamed Joffrey for inciting the starving crowd. When Tyrion realizes that Sansa Stark has been separated from the group and is still outside a petulant Joffrey refused to order his Kingsgaurd to go after her, even though she was a vital hostage. Sansa was nearly raped before Sandor Clegane saved her. When Tyrion tries to thank Sandor for saving Sansa and averting a political disaster, Sandor says, "I didn't do it for you."
Much of the city remained in chaos for some time, with random violence and looting spreading across the capital. The pent up frustrations with Lannister rule and the starvation conditions in the city which Queen Cersei had flippantly ignored for months now came back to haunt Joffrey's court.
In the books
In the books, the Riot of King's Landing is considerably more violent. The party of nobles and their guards are on horseback not on foot. The riot begins after a woman walks out before King Joffrey's horse, halting the procession. She holds up her dead baby, which died from malnutrition. Tyrion sees Sansa says something to Joffrey, and Joffrey callously tosses one silver coin at the woman. It bounces off the dead baby to roll into the crowd, where people fight over it. The woman begins screaming insults at Joffrey and Cersei- shouting that they are a "bastard" and a "brother-fucker", respectively. The crowd takes this up as well and then someone throws manure at Joffrey and he demands the culprit to be executed.
In the ensuing violence, Ser Preston Greenfield of the Kingsguard and Ser Aron Santagar, the Red Keep's master-at-arms are killed. Nine Gold Cloaks are also killed and 40 are wounded. Lollys Stokeworth - the feeble-witted second daughter of old Lady Tanda Stokesworth - is raped by dozens of men, and is found wandering naked and in shock by the Gold Cloaks. Tyrek Lannister, one of Robert's former squires and son of Tyrion's late uncle Tygett, simply disappears, his body never found. Nobody bothers to count all the dead rioters. The High Septon is also torn limb from limb in the book, leading Tyrion to quip that "starving men take a dim view of priests too fat to walk". His crystal crown is also stolen.
The book does not indicate if there was an attempt to rape Sansa: the Hound does rescue her and bring her back within the castle after all of the knights inside refuse to risk going out again, but this chapter is narrated from Tyrion's POV so the details of her rescue are not shown. When she is brought back, she has a deep bleeding gash on her scalp, and tells "They... they were throwing things... rocks and filth, eggs... I tried to tell them, I had no bread to give them. A man tried to pull me from the saddle. The Hound killed him, I think. His arm... he cut off his arm."
After the riots, Tyrion begins a curfew from dusk to dawn, with the penalty that anyone found on the streets will be killed. This measure is horribly unpopular, but curtails the nightly murder-robberies that have been taking place. The growing danger in the city is what prompts Tyrion to bring Shae into the Red Keep for safety. With Varys' help he gets her a position as handmaiden to Lollys Stokesworth, who becomes pregnant after being raped. Tyrion and Varys both try very hard to find Tyrek Lannister, to no avail.
- Riot of King's Landing at A Wiki of Ice and Fire (spoilers from the books)