The 21-gun salute is a worldwide tradition, not a US one, and it's unlikely to have originated there. All countries accord a 21-gun salute to a head of state of any country. This protocol was agreed upon in the nineteenth century. Formerly monarchies were entitled to a higher salute than republics.
A 21-gun salute is performed, for example, whenever a head of state visits another country on a state visit, that is in their capacity as representing and leading their country.
It is carried out with guns in the narrow sense of ordnance, i.e. cannons. In this sense a rifle or a pistol is not called a gun.
The term is sometimes mistakenly applied to the salute given at the funeral of a soldier, in which 21 shots are fired, each of seven fellow soldier firing three rounds.