I definitely second Kalina's wonderful answer that the best defense is using your brain, but I figure this question could use some answers with specific Fallout 4 gameplay answers. That and rudimentary military tactics will turn raids from a problem into a source of minor loot.
First, as pointed out by Kalina, all settlements have a limited number of approaches for enemies. Find them (or use your brain to figure them out), and that's where you need to put your active defenses. No sense in having turrets at the opposite end of any approach. Obviously, bridges, roads, railroads and natural breaks in the terrain tend to be your main source of enemy approaches, so you can focus on building up defenses there before finding out the hard way, if you wish. Building up unmanned defenses also has the advantage of increasing your settlement's defense value, which deters raids from happening in the first place (lowers the chances/frequency of a raid occurring), compared to undefended or lightly defended settlements.
Second, you can do what people have done since prehistory and build walls to keep out invaders. It's not usually necessary to ring an entire settlement with walls (though it is possible, and with some of the smaller settlements, even easy), but you do want to use long walls to funnel enemies towards your active defense systems. And note, I'm talking about walls, not fences. Assume the raiders will be able to jump over anything you can, which makes fences useless for defensive purposes. And on that note, the more solid walls are better for this purpose than the scrappy looking ones that have gaps and holes... because enemies can always shoot through those. (Well, sometimes. You've probably noticed that there are some problems with collision detection in the game engine). The wood shack foundation "structure" (Structures -> Wood -> Floors) makes for a really nice, tall, thick concrete wall, if you really want to give your settlements an impenetrable castle feeling, and are thick enough to mount turrets on top of to boot.
Speaking of walls, if you do try to ring an entire settlement with a wall, you will almost certainly run into the max items per settlement issue, which is indicated in-game by the "size" bar in the top right corner of the screen. You can get around this by scrapping extraneous structures and items, as well as by an exploit - if you drop any gun and then store it in your workshop, the game engine will count that as reducing the number of items in the settlement by 1. (This only seems to work with guns, not other types of items).
Third, speaking of walls and active defenses, it is generally tactically advantageous to fire from an elevated position, and this applies in Fallout 4 too. So don't build turrets on the ground, build them on an elevated position, either naturally occurring, or constructed. (You can always build temporary ladders and stairs to reach high spots.)
I will try to have my turrets either on the top of my defensive walls (by using the wood shack foundation walls, or building my defensive walls as multi-story buildings and using the roof/floor of the top level) or placing the turrets further back, atop an even higher structure. See below for an example with the Red Rocket "settlement," that I use to store my collection of possible companions. I've had a couple raids here, and each time, the super mutant raiders just ran back and forth in front of the wall while my turrets chewed them to shreds.

Fourth, you can, and probably should, arm your settlers. What else are you gonna do with the literally hundreds of pipe guns and suits of raider or leather armor you'll come across in the wasteland? "Trading" them to a settler is a good way to have a well armed population to repel anything that manages to limp through your automated defenses. (If you give them a gun, they will have unlimited ammo for it, so long as they also have at least one bullet for it in their inventory as well.)