The U.S. Navy has a lot of rules regarding saluting.
Navy personnel are required to render salutes to superior officers, but don’t salute if uncovered (i.e. without a hat). Since Navy personnel rarely wear uniform headgear indoors, saluting is rare indoors (or below decks on a ship).
Covers are worn when outdoors, so salutes are rendered to officers when greeting a superior officer outside (and a greeting is required along with the salute). The officer is required to return the salute and greeting.
When coming aboard or departing a Navy vessel, you render a salute to the national ensign (i.e. U.S. flag) and the Officer of the Deck (if present). This also applies to foreign vessels.
Above decks aboard a ship (if covered), you salute the captain, any officers senior to the captain, and flag officers (i.e. admirals) every time you meet them, and more junior officers only upon the first daily greeting.
At sea (aboard a vessel outside the harbor limits), headgear/covers need not be worn, so saluting is not done then either. In other words, nobody usually worries about saluting when Navy personnel are actually doing their job at sea.