Modern (post Korea basically) fire control radars and increasingly digital and networked CiC's mean that each weapons system is more efficient in when were and how it engages.
And most of all the primary punch of the modern surface combatant is the missile not the gun. The modern DDG will be able to engage targets out to 2-300 miles based on the AEGIS AN/APY radar family and the Standard Missile(SM), Harpoon, and Tomahawk triad.
For close defense missiles are still a go to, the Sea Sparrow and descendants offer a major advantage over traditional AA fire, they can adjust their flight path.
The need then also for heavy Naval Gunfire Support is also mitigated by smart bombs, larger aircraft payloads, drones, and especially cruise missiles which can be carried on the ship. The need and role does still exist for limited gunfire support or direct action, hence why they are not done away with completely, and future platforms like the Zumwalts offer potentials for rocket assist6ed shells, which can adjust their trajectory, or in moving completely to railguns moving forwards. And a CIWS is still useful as a last line of defense to put lots of lead between a ship and missile, and small mounts for M2 machine guns for instance are useful when in port or in congested waterways to ensure ship security.
Not sure if you mean anti-ship guns or anti-aircraft guns, but I believe the cause is the same. Missiles have shown a tremendous ability to increase the range and lethality of surface ships. Such progress could not be achieved through the advancement of ballistic technology. For example, the largest gun on the USN's most advanced WW2 destroyer (the Gearing class) had a maximum range of 16,000m (horizontal) and 11,300m vertical. The modern Arleigh Burke's largest gun's maximum range is about 24,000m. Ok, so that seems like double which is a nice range increase, right? The Arleigh Burke, however, also has the Tomahawk missile which can hit targets an astonishing 2,502,052m away, with the added bonus of GPS guidance and the ability to be reprogrammed for a new target in-flight. They also have surface-to-air missiles that go far beyond the range of guns and can automatically engage multiple targets without the need for a gunner/operator. TL;DR - Missile technology has allowed naval vessels to become far more lethal using far fewer numerical assets.