My first forts all died that way - an Ambush comes out of hiding next to the main staircase while your woefully small military is either drinking or sleeping.
Tipps to prevent this:
a) You want a safe outside area around your fort entrance. Early on, preferably in the first year, try to make it so anything approaching your fort will have to go through some chokepoints.
b) When you get immigrants, turn one into a Mechanic and one into a Mason, unless you have these already.
c) I've had fewer random deaths since I build walls around a large "courtyard" area, so you can have outdoors farm plots, a fishing area, refuse pile for anything except bone&shell&skull ... surrounded by walls. I usually build 2 or 4 gates.
d) Surround the walls with channels, so that "building destroyers" cannot reach them. Build bridges over channels and add levers so you can open and close each gates, making your fort pretty much impenetrable to ground units.
e) Build another wall in front of the gate, to prevent marksgoblins who approach the gate from shooting into the fort. Just maike sure the paths are all 3 tiles wide.
f) Fill the approach to each gate with traps. I usually have an area covered in weapons traps with 2 wooden or glass spiked balls each, and a few cage traps further out.
g) Add more traps near the walls. When invaders come from the South, just lock all gates keeping the North Gate open, and they'll path through the majority of your traps.
... unless you modded the game, with this approach you hardly need a military except to dispose of the occasional Kobold. I built the above, and have 75 military and a full guard ... the fort is now in its eighth year and has not had a siege yet, only two ambushes :/