Starfleet ships are armed for battle because they’re out in the unknown and untamed frontier, not within the safe borders of the Federation that’s far behind them. At least that’s how it was often depicted in the original series, where it was explicit policy that stories would never take place on boring future Earth (I kinda hate later movies and series for forgetting this crucial policy).