To travel as an armed guard next to a vehicle's driver. Latterly, (chiefly in the USA) - to travel in a car's front passenger seat.
Please Sir, I know this one. It's from the wild west stagecoaches, which had guards armed with shotguns to protect them isn't it, or was it English stagecoaches to protect them from highwaymen? Whoa, there pardner. Maybe that's what people think, but there's no evidence to place this phrase that far back in history, in the USA or England.
The reference is to the US stagecoaches that were an essential feature of Hollywood westerns - usually being chased by Indians or bad guys in black hats. In the 1939 classic film Stagecoach, George Bancroft plays Marshal Curly Wilcox who is featured riding shotgun in screens throughout the film, to protect the coach from the pesky Apaches. He mentions the term explicitly in the dialogue:
"You boys take care of the office for a couple of days. I'm going to Lordsburg with Buck. I'm gonna ride shotgun."
The earliest reference I can find in print to people riding shotgun in real life is from the Utah [where else?] newspaper The Ogden Examiner, May 1919 - headed "Ross Will Again Ride Shotgun on Old Stage Coach":
Driven by Alex Toponce and A. Y. Ross, an old fashioned stage coach made in 1853 and used on the Deadwood stage line in the early days of Wyoming, will appear in Ogden streets on the day of the Golden Spike celebration.
Alex Toponce was in the early days the owner of a stage line. He will probably drive the old fashioned vehicle, while A. Y. Ross, famous in railroad circles as a fearless express messenger and who on several occasions battled with bandits on the plains, will probably ride "shotgun" as he did in the past.
Express messengers, like A. Y. Ross were also called shotgun messengers and the guns they used were called messenger shotguns.
Long distance stagecoaches ceased to be used soon after the introduction of the transcontinental railroad in 1869. Short haul coaches lasted a little longer but their use was also in steep decline by 1900 and they disappeared when motor vehicles became available early in the 20th century.
There is good evidence that people were employed to guard stagecoaches on early USA stagelines. In October 1891, the Iowa newspaper The Oxford Mirror, published this comment:
"Of all the devices and inventions for the protection of treasure and circumvention of the road agent, the only one that has stood the test of time and experience is a big, ugly-tempered man with a sawed-off shotgun on the box."
It seems quite plausible that the term riding shotgun would have been used, but it appears that it wasn't - not until well after stagecoaches had gone out of use and people started making westerns. Although we have 20th century references to people riding on stagecoaches with shotguns from films and newspapers, there are no accounts from the 19th century that call this riding shotgun.
The 1950s saw a spate of TV cowboy series in which riding shotgun was so commonplace in the scripts as to be almost obligatory. The term was taken up by US teenagers when referring to riding in the front passenger seat of a car. It became a game to shout "I call shotgun" to reserve the front seat - which was generally seen as being the premium position (although, in those pre-seatbelt and air-bag days, probably the worst choice). This was shortened in the 1960s to just shotgun. The term in that context is restricted to the USA.
The figurative use of the earlier version of the term was in common use by the 1980s. Here's an example from The [London] Times from January 1980:
"It was quite by chance that The Times found itself riding shotgun for the Red Army."
Riding shotgun in the real sense has moved on since the days of stagecoaches. VIPs who travel in limousines are now often flanked by several motorcycle outriders. This has also been adopted in a figurative sense and supporters of political figures, especially those who aren't employed directly as spokespeople but who feign independence in order to give the appearance of a politician's wider appeal, to are often now called outriders; for example, The Morning Star, September 2006:
Tony Blair's outriders have floated the idea of a "which direction for Labour?" debate.
Since the September 2001 terrorist attack in New York, US airlines have begun employing air marshals to protect the planes in flight. These have widely been described as riding shotgun. They don't carry shotguns of course, that would be rather counter-productive in pressurized cabins at 30,000 feet.