Starfleet seems to have a problem with keeping their ships safe. In a fight between ships, a Starfleet ship can usually hold its own. The problems begin when someone gets on the ship. Starfleet Security has demonstrated time and time again that it might be just a little bit too trusting. Starfleet ships have been hijacked by just about every group imaginable. The Maquis, Klingons, Romulans, and Bynars are all among the many powers who have successfully commandeered Starfleet ships. The flagship of the Federation itself was once taken over by a group of Ferengi in a few surplus Klingon ships.
It doesn’t even end there. Security protocols are easily bypassed by the officers and civilians aboard the ship. If you want to avoid being tracked, all you need to do is remove your combadge. At one point, a civilian was able to escape the ship in a shuttle without assistance or permission from anyone. People could be replaced by changelings for months and avoid having their identities or their acts of sabotage being discovered.
However, not every ship had this problem to the same degree. There are some threats that can’t be prepared for. No security is entirely foolproof!