Bayonets were first made in the French town of Bayonne, from which they draw their name. They are basically knives, swords or spikes that attach to the front of rifles.
Early rifles fired very slowly, as they only held one shot, and they had to be loaded from the muzzle. In the days before bayonets, riflemen had to be protected by pikemen, wielding long poles that horses won't charge at. This prevented formations of riflemen from being attacked by cavalry.
The first bayonets were wooden plugs that could be placed in the barrel of a rifle. If cavalry charged, they could be fixed onto barrels, allowing riflemen to double as pikemen. This eliminated the need for pikemen, halving manpower requirements, and hence doubling the mean firepower of infantry units.
The bayonet was keenly adopted by the British, who used it around 1745 against the Scottish uprising. It was quickly enhanced from the 'plug' bayonet, which prevented firing, to the 'socket' bayonet, which allowed it. Fighting the Scottish, however, it proved ineffective against the highland charge.
The British developed drills and techniques for using bayonets, partly based on the highland charge. They would advance towards the enemy quickly, and fire from a number of meters away. This would create a big cloud of smoke, as well as killing enemy soldiers. The British infantry would then charge out of the cloud of smoke, towards the enemy. This was designed to be particularly frightening, and was used to great effect in the napoleonic wars, and in maintaining British colonies abroad.
In the First World War, British generals attempted to use bayonet charges, such as in the battle of the Somme. They were brutally cut down by enemy machine guns.
In modern times, the bayonet isn't put to all that much use in battle. It is still popular as a training weapon, as it encourages courage and aggression; bayonet practice is used as 'battle inoculation'. It was also used briefly in the Falklands war, mainly as a psychological weapon.
In modern times, bayonets are not often used in battle. They are, however, cheap and sometimes useful, and they remain a staple of most soldiers' inventories.