Edit: and crossbows of course
The earlier 16th c. is when firearms, matchlocks, become a standard part of the European battlefield. By 1525 and the battle of Pavia, it was obvious that they were pretty indispensable. However, while they had definite immediate advantages over both long bows and crossbows, they could still only be loaded and fired at a slow rate, and were not that effective past 200 meters. As a result, after only several volleys a pitched battle would typically come to a point where the armies would close. Then the muskets would be useless, as close-in weapons became effective: the soldiers with muskets would use them as clubs, fighting alongside the pikemen.
In the later 17th c. a bayonet was developed that did not plug the bore, and the musket then could be turned into a pike. And because muskets continued to have a limited range, and were slow to load and fire, armies would still typically close. and the fighting still would become hand-to-hand. So, the bayonet was still very important.
The development of longer range weapons with a faster rate of fire in the 19th c. made the bayonet charge less and less effective- notoriously in WWI- and increasingly rare after that, as frontal assaults became more and more costly for the attacking force.