The first bayonet was stored in front of the gun. Then it was placed under the gun's barrel. Over time the bayonet was designed to be located on the side of the gun.
The bayonet was used for hand to hand combat. The bayonet protected you when the enemies came too close while reloading the gun.
Bayonets are not as useful anymore because of automatic guns. Automatic guns are more efficient and accurate.