This seems to be a common characteristic of naval battles. Ships were often able to bombard land artillery while staying out of range of return fire. Is this because of something intrinsic to putting a cannon on a ship or have I just disproportionately been reading about battles where this happened to be the case? What am I missing?
For reference, a couple example battles were the Battle of Port Royal in the American Civil War and the Bombardment of Fort McHenry in the War of 1812.
Your two examples are somewhat unusual. The forts in Port Royal were armed with relatively small-calibre guns because the Confederates were scrambling to put together what they could, and short on heavy guns all over the place. Ship-killing artillery at the time was north of 9 inches; Fort Walker had only a single 10-inch gun, and some smaller pieces. Moreover, to cram in additional small guns to try to make up the difference in throw weight, the siting of the battery was changed from the original plan, and the Union ships were able to sit in positions that fortress guns didn't reach and bombard it. So, we've got ships with heavy artillery bombarding a fort with pieces too small for a naval battle, and badly sited so that in some cases it couldn't shoot back. Observe that field artillery of the time was not a ship-killing weapon; guns that could be moved by horses or at a pinch by manpower were just not heavy enough to do much damage to a heavily timbered warship.
Nelson claimed that "A ship's a fool to fight a fort". I think, though, that Nelson was considering forts fully manned, equipped with the best guns, and generally speaking really ready for the fight. It could be the case that such forts are rare in history, because it takes time to build them and effort to maintain them at a high state of readiness; in peace there's better uses for the money and when the war comes you have to scramble! Then, battles against such forts might be even rarer, because naval officers don't like to go up against them. So it could be the case that battles between ships and forts will disproportionately find improvised, badly-supplied, or obsolete forts, going up against modern ships. Another example is the battle of Kinburn, during the Crimean War, where ironclad French "floating batteries" were able to pound Russian coastal forts to pieces because, basically, they were immune to round shot. So we see that the forts are obsolete because of a technical advance they haven't been prepared for. Similarly during the German invasion of Norway, the coastal forts mostly did try to fight, in spite of a total lack of orders from the government; and sure, they famously sank the Blucher, but you'll note that the invasion went ahead just the same and the forts fell to landing parties pretty quickly. So that's what happens when you have forts built to resist a Swedish invasion in 1905, and not fully mobilised for war at that, going up against warships from the thirties with their ancient torpedoes. Except for that one lucky torpedo hit on Blucher it was a bit of a disaster. At Bergen, Kvarven Fort got off only a few shots with its guns - the torpedo tubes weren't fired, apparently because the fort was not fully mobilised and the torpedoes were missing their fuses!
An example of land fortifications winning against a purely naval force is the Gallipoli campaign, where the British originally tried to force the Dardanelles with just some expendable battleships. Which turned out not to be so expendable after all when they ran into mines. The Turks didn't have the very latest and greatest, but they had enough to stop ten battleships from forcing the Straits; the British eventually gave up on the purely-naval operation and landed troops instead to silence the batteries that were covering the minefields.
And the rocket’s red glare, the bombs bursting in air, Gave proof through the night that our flag was still there.
The British navy did not use cannon against Fort Mchenry on the evening that inspired Francis Scott Key. They used rockets and "bomb" ships equipped with mortars that fired explosive shells at a high trajectory towards the target.
The problem is that the explosive shells, while long ranged, were not so effective against a target like a fort. As Mr. Key noted, Fort Mchenry did not fall to the British.
The mortars, which produce shrapnel when they explode, would work better against civilian targets (or infantry) but Baltimore was protected by fortifications armed with cannon that prevented enemy ships from getting close.
Land based cannon in a fort can be much larger/longer ranged than those in a ship because one does not have to be concerned with weight on land. In addition, accuracy is better because ground does not move like the decks of a ship.
The answer to this is essentially purely logistical in nature. A longer range on a gun requires a "bigger" gun. So moving round large guns with a land based army would either slow the infantry and cavalry down, reducing their ability to respond, or coming so late as to not have an effect on the battle. This also extends to fortifications, as before mechanical assistance, it would be very difficult to get a large calibre, heavy artillery piece mounted into fortifications.
Placing a large piece of artillery on a ship was essentially the end of the problem, as they were in fixed positions, and did not need to be moved once on the ship.