The exact origins of the phrase are difficult to pin down, but it’s generally attributed to the practice of biting on a bullet during warfare to deal with pain or discomfort.
According to one theory, soldiers would bite bullets during field amputations during the Civil War. Patients would either die or “bite the bullet" and undergo a horrific operation. The lead balls, while not exactly nutritious, would absorb the bite without damaging their teeth.
While some believe this arose during the Civil War, when field amputations were common, etymology blog The Phrase Finder notes that Civil War surgeons would have had access to ether and other anesthetics—soldiers wouldn’t be forced to bite bullets, sticks, or other items. Besides, the phrase predates the Civil War.
Another theory: Musket ammunition was supplied to gunmen in a paper cartridge. Half the cartridge contained the musket ball, and half contained the gunpowder that the musket ball needed to fire. To load their muskets, gunmen would bite the cartridge open so they could pour in the gunpowder and jam the ball in behind it.
While this seems like a plausible origin of the phrase, there’s nothing especially painful about biting off the top of a paper cartridge. Perhaps the phrase started among soldiers who were trying to ready one other for battle: “Just bite the bullet, and we can start fighting.”
There’s another possibility. In the 1788 text A Classical Dictionary of the Vulgar Tongue, lexicographer Francis Grose—himself a former soldier—suggested the phrase came from military punishments. Here’s Grose’s definition of a “nightingale,” an antiquated phrase for a coward.
“Nightingale - A soldier who, as the term is, sings out [during corporal punishment]. It is a point of honor in some regiments, among the grenadiers, never to cry out, or become nightingales, whilst under the discipline of the cat-of-nine tails; to avoid which, they chew a bullet.”
The cat-of-nine-tails was a whip used for corporal punishment. Soldiers bit bullets, which could be easily concealed in their mouths, to avoid crying out (and thus becoming “nightingales") during their floggings.
This appears to be the most likely answer, although it’s possible we don’t have a complete history. Perhaps we’ll just bite the bullet and acknowledge that we can’t truly know the origins of this idiom.