The Security Department is a branch of Starfleet that deals with all security issues to the Federation and Starfleet, the main security is split into two parts, Internal and External. Internal Security deals with Security threats that are aimed internally at Starfleet and External deal with outside threats to the Federation.
It is the same for a ship or Starbase and Outpost, on the Tomcat both aspects are there, but Security mainly works with the Marine units on the ship to help defend her from boarding Parties, the ships Intelligence Department deals with gathering information to the external threats to the ship.