I apologize if this is a weirdly specific and obscure question, but I'm currently building a model of the USS Missouri, and one thing that caught my attention was the sheer number of medium sized gun batteries it had (bigger than anti-aircraft caliber, smaller than its main armament).
Having built a few other models as well (USS Arizona, Yamato, etc) it seems like battleship designers were constantly trying to pack in as much armament as possible on these ships, but it seems to me that anything smaller than the main armament which wasn't useful against aircraft seems to have no purpose.
Considering the sheer range of the main armament, it seems to me that the battleships would be engaging at close to maximum visual range with their main armament, and would probably never get close enough to give a "broadside" of 5 inchers for example. Also the fact that doctrine had smaller ships screening the battleships makes me feel like these batteries were probably never used.
What was the purpose of these batteries and were they ever used to good effect in combat?
The secondary batteries - the 5 to 6in guns to which you refer - had several purposes. It was common, especially towards the end of the battleship period, for them to fulfill a dual duty. The RN referred to such guns as HA/LA guns (High Angle - Low Angle), because they were useful against both surface targets and air targets. Secondary batteries of this type were used and useful throughout WW2, as they made up a significant part of the battleship's air defence.
The secondary guns needed to be useful against surface targets, due to the risk of destroyer attacks. These attacks would bring destroyers into close range, even with the screen being present. If this was during a fleet action, turning all or part of the main battery onto the destroyers was a poor option. Doing so would prevent damage being caused to what was arguably a bigger threat. However, the destroyers couldn't be ignored, so extra guns were needed to protect against them. Having bigger guns wasn't ideal - they trained and fired more slowly, and big guns really aren't needed to sink a destroyer. Jutland demonstrates this use well, especially during the night action. It must also be remembered that battleships aren't always engaging in perfect conditions. At night, or in poor weather, ships can reach into very close ranges before they were engaged. During both the night action at Jutland in WW1, and the night naval battles off Guadalcanal in WW2, battleships used their secondary armaments against close-range targets. At the Second Naval Battle of Guadalcanal, the American Washington and South Dakota engaged the Japanese Kirishima at close range, with both sides using their secondary batteries. This after action report estimates 20 5in hits on Kirishima, from American secondary batteries.
The secondary armament was also useful for firing other types of ammunition. In a night action before radar was developed, having star-shells to light up a prospective target was highly useful. At the Battle of North Cape in 1943, Duke of York used star-shell fired from her 5.25in secondary battery to illuminate Scharnhorst, while sinking her with her main armament.