You need to get the nation mad at you in order to attack it from the sea. You can anger them by capturing their ships and by shooting at the town. You can tell they're mad at you because the town will shoot at you as you sail near.
Whether you attack from sea or land, you need to have sufficient crew to make it possible you'll defeat their armies. You can usually see the size of the garrison in the town by looking at the map, and then clicking on the red dot for that town on the map. If it DOESN'T show the size of the army, you may need to capture a ship near that town, and the crew of that ship may know the size of the garrison and therefore make that info available on the map.
If you don't have sufficient crew, you won't get the option to attack when you try to enter the town from land or sea. If you DO have sufficient crew, you'll be asked whether you want to attack, and the question will show you the size of their army.
And as said in the other answer, you can attack from land even if the nation isn't mad at you.
Also, you can't attack missions or indian settlements or pirate havens or 'settlements' that are randomly placed when you start the game, you can only attack cities/towns that are a permanent part of the map for the era in which you are playing.