There are a few great tips and strategies to ensure this is avoided at all costs.
First, and by far the most important, is to scout. Before every enemy encounter, you are able to spend a very reasonable amount of Coins to view the enemy base before committing to an attack. At this point you need to make the decision as to whether the attack is worthwhile - what is the risk of losing all of your troops but not getting the victory? You should also examine what strategy you hope to employ when it comes down to the attack. While you have a good amount of time to be able to plan during the attacking phase, there's no harm in doing so during the scouting as well.
Pay particular attention to the location of the enemy HQ, and the layout of their defenses. Often you will be able to move your troops round any defenses without taking many casualties if the enemy haven't planned their base very well.
You won't have too many problems with the early enemy bases
The Gunboat's artillery strike is one of the most satisfying elements in attacking the enemy base. Picking out what to attack and how to use the Gunboat's power-ups does require a good deal of insight. Artillery and Barrage are the most popular and most common attacks. Use these to attack Cannons and Boom Cannons before they have a chance to fire on your troops.
If you have lower-level troops such as Riflemen and Zookas, however, you need to focus your Artillery and Barrage on different structures. Rocket Launchers pose the greatest threat, followed by Mortars and Flamethrowers. Sniper Towers, Boom Cannons and regular Cannons also shouldn't be ignored. Rocket Launchers are most dangerous due to their high splash damage and range - they will take out most of your units before they even come close.
With all this said, the Gunboat has other ways to mitigate the threat of enemy defenses. Use Flares to order your troops either to avoid specific defenses, or to attack one en masse - rather than waiting for them to pick the nearest target. You can also use them to send your troops directly to the HQ. When used with Shock Bombs to temporarily disable enemy defenses, many players have started using Warriors to rush the HQs.
Use Smoke Screens and Shock Bombs to protect your units while they are on the move - you can basically guarantee your army's safety up to a certain point by employing these tactics. Just be aware that you will have to decide whether to use Artillery/Barrage or save some Energy for troop directions.
Use Flares to direct your troops around the enemy defenses
Rush the HQ
As mentioned above, HQ rushing is a great tactic for getting a quick end to the battle, though it will often result in high losses to your troops. Most players will choose Warriors for this tactic. Obviously the idea is to scout an enemy, then train lots of Warriors, before attacking. Use Flares to designate the HQ as the target and, if necessary, a route around the most prominent enemy defenses to get there. Back this up with Shock Bombs and Smoke Screens to protect your attacking force while it destroys the HQ. With enough troops you should be able to complete the rush in most cases. Focus your Shock Bombs on Cannons, Boom Cannons and Sniper Towers that are in range of the HQ.
Similar to rushing the HQ, this technique relies on building as many cheap troops as you can - generally Riflemen. With all the enemy defenses overwhelmed as to which units to target, your troops will often be able to make real progress - also gathering more Energy to support more Gunboat attacks. The real threat with this tactic is if the enemy employs defenses with good area of effect attacks, as this can potentially take out much of your army with one stroke.
This involves sending your units around the back of the enemy base - to which most users are very vulnerable. Use Heavies and Riflemen directed by Flares to reach the often undefended rear.
The key to this tactic is choosing units with high health to protect those with lower health - the units that typically can cause the most damage. Send the high-health troops (such as Heavies) a few seconds in front of the low-health ones (Zookas are the most common). The high-health troops soak up the enemy fire and allow the prized but weaker units a chance to get their shots away. This tactic works best with enemy bases that have just a few high damage defenses - if there are too many then the shield will not be able to protect all of your units.
Most players' defenses are weak at the rear
By far the most fun to be had in Boom Beach is from attacking enemy islands with your forces. However, there isn't much more infuriating than spending all your money and time training troops only to see them wiped out by the enemy's defenses.