Let me update this with information gleaned from hours (minutes) of research in a giant library (wikipedia).
Bayonets were first introduced in the early days of firearms, when they were grossly inaccurate and took upwards of a minute to reload. In these days, cavalry and infantry charges were still an effective tactic. Spears had long been the effective counter to those - a charge's effectiveness is based on momentum, which spears turn back on your enemies. Since carrying around a giant spear would almost be worse than not having one, and since all the soldiers were already carrying long rods of wood and metal, it made sense to strap a spike on the end of it and turn it into an instant spear.
As guns became more and more accurate, charges slowly became less effective. Once you had to deal with rifles that were incredibly accurate and mounted weapons that could spray hundreds of bullets a minute, coming out of the trenches became a BAD idea. By this point, advances in anti-infantry weaponry made charges obsolete. Modern bayonets are mostly useless since nobody in their right mind would mount an infantry charge against handheld automatic weapons and in hand to hand combat they're too bulky compared to shorter knives or clubs. They aren't a bad weapon, they're designed for a style of war that no longer exists.