It is a gesture of respect to a person of superior rank. It is formalized that is, it is done in a certain way every time. Salutes of all kinds have existed in all periods of history and in all cultures. The form of salute has varied. In some cases it is meant bowing, in others it means kneeling or laying on the ground or some various gestures of hand and arm. The individual military salute that a soldier gives, raising the right hand to the forehead or to the hat brim or visor was developed quite recently in the history.
Until the end of 18th century the way junior officers saluted superiors and soldiers saluted officers was to doff the hat. In fact, civilians still do this as a gesture of respect and this custom probably goes back to the days when a knight would raise his helmet's visor or uncover his head before a lord.
The change in the style took place for a very practical reason, when soldiers fired their muskets, black powder would settle on their hands and make them very grimy. If they then salute the old way it would ruin the hats. So at the end of 18th century the change was made to the hand salute.
Until the end of 18th century the way junior officers saluted superiors and soldiers saluted officers was to doff the hat. In fact, civilians still do this as a gesture of respect and this custom probably goes back to the days when a knight would raise his helmet's visor or uncover his head before a lord.
The change in the style took place for a very practical reason, when soldiers fired their muskets, black powder would settle on their hands and make them very grimy. If they then salute the old way it would ruin the hats. So at the end of 18th century the change was made to the hand salute.