One way to tackle this problem was to organize the troops into three groups and each group would move forward, fire and then retreat to the back of the line to reload. This meant that one group would always be prepared to fire, although it reduced the firepower to a third of the strength of the unit. It didn't work so well if there were less people in the unit and also didn't work very well for hunters, who often operated solo or in very small groups.
Another solution was to carry a second weapon to protect themselves during close range fighting. Often, this second weapon was some form of short sword or knife. In 17th century France, people started to make knives that could be attached to a musket, thereby converting the musket into a sort of spear. There is an old legend that the inhabitants of the French town of Bayonne had run out of gunpowder and bullets during one conflict, and attached their long hunting knives to their muskets, and the word "Bayonet" comes from the name of their town "Bayonne". Another possibility is that hunters from that area used to carry such devices to protect themselves, in case their shot missed the target. Whatever may be the reason, it is generally accepted that the bayonet seems to have originated in southern France around the end of the 16th or early 17th century.
A 12-18 inch (or 30-45 cm) bayonet knife at the end of a 5 foot (about 1.52 meters) long firearm makes it about the same size as a pike or a spear, but it is much heavier than a corresponding spear or pike or any other polearm type weapon. However, it was better than nothing. People could shoot their muskets and then fix their bayonets and charge at an enemy position (the classic "bayonet charge") or form defensive squares against enemy cavalry (such as that done during the Battle of Waterloo).
The first bayonets were of the plug type. These had a round handle that could fit into the muzzle of the firearm, plugging the end of the barrel. This was useful because the user could use it independent of the musket as a long knife, but could also plug it into the musket and use them together as a polearm. The major disadvantage of this type was that once it was attached to a musket, the weapon could not be fired until the bayonet was removed.
A plug bayonet. Click on image to enlarge.
Plug bayonet attached to the end of a flintlock musket. Click on image to enlarge.
Note that the musket cannot be used as a firearm since the bayonet is plugging the barrel.
This disadvantage was noted by the French early on and they developed a socket bayonet that could be fitted over the muzzle of the musket, thereby allowing it to be used as a firearm as well. However, this type of bayonet was not really accepted in the late 1600s and plug bayonets were still more popular.
A socket type bayonet from the early 19th century. Image licensed under a Creative Commons Share-Alike 2.0 France License by user Rama at wikipedia.
The first time that any army used it as a weapon on British soil was at the Battle of Killiecrankie in 1689, where William of Orange's troops faced Jacobite Royalists. Unfortunately for William's troops, they were using plug bayonets and since they could not fire once the bayonets were attached, their enemies charged after that and won the battle. This caused the defeated commander, Hugh Mackay, to develop his own form of socket bayonet that could be attached around the barrel.
Troops using socket-type bayonets. Click on image to enlarge.
Early socket type bayonets had a problem of properly attaching it to a musket, as they had a tendency to fall off the end of the barrel during the heat of battle. This problem was solved later on by making bayonets have spring loaded locking systems that attached to the front sights of the firearm, thereby locking the bayonet into place. Socket bayonets stayed popular for a very long time.
Towards the end of the 18th century and the 19th century, as rifles began to replace muskets, the socket bayonet began to be replaced by the sword bayonet. This form of bayonet is also attached under the barrel like a socket bayonet, but it has a suitable handle to be used as a separate sword, similar to a plug bayonet. These bayonets started to come into use as rifles began to replace muskets in various military units. As rifles were often shorter than muskets, they needed longer sword bayonets so that when they were attached, they would be about the same length as a musket with a socket bayonet. One of the advantages of this over socket bayonets is while socket bayonets were only sharp at the tip and converted rifles into spears which could only be used for thrusting, sword bayonets also had sharp blade edges and converted rifles into glaives, which could be used with both thrusting and slashing movements.
An Indian made Ishapore 2A1 Lee-Enfield rifle with attached P1907 sword bayonet.
Click on image to enlarge. Public domain image.
Sword-bayonets had the advantages of both socket type and plug type bayonets with neither of the disadvantages of the two. However, during World War I, it was discovered that sword bayonets could not be used effectively inside trenches where space was limited. Hence, after World War I, the trend has been to reduce the length of the blade, so that modern bayonets are about the length of a knife instead of a short sword. The first knife bayonet was actually developed before World War I, by the Germans, who developed a 10 inch Seitengewehr bayonet in 1871 and used it till 1884, where it was followed by the Seitengeweher 1884 model which was used till 1945 in German service. Several other militaries began to use knife bayonets after World War I. The vast majority of bayonets developed after World War II are knife bayonets.
US Military Bayonets of World War II. Notice how the size decreases from short-sword length to knife length
Click on image to enlarge. Image licensed under Createive Commons Attribution Share-Alike 3.0 license by author Curiosandrelics at Wikipedia.
During the period after World War I, many nations also began to make multi-purpose bayonets, wherein the weapon could not only be used as a knife, but more like a multi-utility tool. The Germans developed knife bayonets with saw-teeth on the back spine, so that they could be used to cut meat as well as saw wood to build defenses, cut tent poles etc. Some other bayonets were developed with a sort of spade to allow digging trenches. The latest bayonets in the US military inventory, the M7 and M9 used by the US army and navy and the OKC3C used by the US marines can all be used as general purpose knives. The M9 and OKC3C also incorporate wire cutters as well.
US Marine Corps OKC3C knife bayonet. Click on image to enlarge. Public domain image.
US Military M9 bayonet attached to M4 carbine. Click on image to enlarge.
Image licensed under Createive Commons Attribution Share-Alike 3.0 license by author Curiosandrelics at Wikipedia.
As can be seen in both the images above, both models of bayonets have saw teeth as part of one of the edges. In combination with the sheath, they can be used as wire-cutters. They can also be detached from the rifle and used as a separate knife. The OKC3C is the newer model and is heavier than the M7 or M9 and can penetrate modern body armors better than the other two.
Lest readers think that bayonet charges died in the Napoleonic era and modern bayonets are more used for utility tools these days, it might be useful to remind readers that bayonet training is still part of the curriculum of most military forces around the world. Bayonet charges have been used well into the 20th and 21st centuries as well. The last major US bayonet charge happened during the Korean war, (led by Lewis L. Millet Sr., who won a Medal of Honor for this action), but there have been cases of forces using this tactic well after that. Recent examples include the Scots Guards (British army) in Falklands against the Argentinians in 1982, various Indian units against Pakistanis in Kargil in 1999 and the Argyll and Sutherlanders (British army) against the Mahdi army in Iraq in 2004. As recently as 2010, there were incidents of Gurkhas and Scots using bayonets against the Taliban.