Salute by cannon or artillery is a military tradition that originated in the 14th century. The 21-gun salute, commonly recognized by many nations, is the highest honor rendered. The custom stems from naval tradition, when a warship would signify its lack of hostile intent by firing its cannons out to sea until all ammunition was spent. The British navy developed the custom of a seven-gun salute because naval vessels typically had seven guns (and possibly also due to the number seven's Biblical and mystical significance). Because greater quantities of gunpowder could be stored on dry land, forts could fire three rounds for every one fired at sea — hence the number 21. With the improvement of naval gunpowder, honors rendered at sea increased to 21, as well. The 21-gun salute eventually became the international standard.
In the United States, the custom has changed over time. In 1810, the War Department defined the "national salute" as equal to the number of states in the Union (at the time, 17). This salute was fired by all U.S. military installations on Independence Day and whenever the president visited a military installation. In 1842, the 21-gun salute was designated as the "presidential salute," and in 1875 the United States followed Britain in adopting the 21-gun salute as its international salute.
Today, the U.S. military fires a 21-gun salute in honor of a national flag, the sovereign or chief of state of a foreign nation, a member of a reigning royal family, and the president, ex-presidents and president-elect of the United States. The 21-gun salute is also fired at noon on George Washington's birthday, President's Day, Memorial Day, Independence Day, and the day of the funeral of a president, ex-president or president-elect.
Gun salutes for other U.S. and foreign military and civilian leaders vary in number, based on protocol and the honoree's rank. These salutes are always in odd numbers.
The 21-gun salute is not to be confused with the three-volley salute (or three-rifle volley) rendered at military honors funerals, which you might see or hear at Arlington National Cemetery.