Nobody knows for certain why the pirate flag is called the Jolly Roger.
The name is recorded in print by 1724. The Oxford English Dictionary cites A General History of the Pyrates, signed "C. Johnson," as the earliest use. The British Journal of 22 August 1724 also used it, in the line "On the hoisting of Jolly Roger, (the Name they give their black Flag,) their French hearts failed." The author of the Pyrates book is disputed, though it has often been attributed to Daniel Defoe.
Etymonline says the origin of the term is unknown. In this phrase, "jolly" has an obsolete sense, "high-hearted, gallant." Why that word was paired with "Roger" is where the argument begins.
One theory is that it comes from the French joli rouge, "pretty red," for a red flag pirates sometimes flew. Hudson Valley's history site notes that some scholars hold this view. No early source is known to show it, and one researcher writing for the American Dialect Society finds it unconvincing because the 1723 flag was black, not red.
The other theory has better documentation. A London newspaper report of October 1723 described the execution of 26 pirates in Rhode Island that July. Their black flag, showing an hourglass, a dart through a heart and three drops of blood, was fixed to the gallows. According to the report, the pirates called this flag Old Roger and said they would live and die under it.
"Old Roger" also appears as a name for the devil, which the OED first records in 1725. Etymonline adds that Roger was occasionally used for the Devil, like "Old Nick." A skull-flag named for the devil makes sense, although the 1723 report never uses the words "Jolly Roger," so the link is an inference.
A common assumption is that the Jolly Roger is always a skull and crossbones. Early descriptions do not fit that picture. In the 1723 report the flag shows Death with an hourglass, and Edward Low's flag was a white skeleton with a dart and an hourglass. The name seems to have covered many designs.
For the name "Roger" itself, Etymonline says it comes from Old French Rogier and Old High German Hrotger, roughly "famous with the spear."