On the question of "why not just use a freighter" I think there are a few considerations here.
There are a few situations where you want a more capable ship that can carry a fair amount of stuff, even though sending a warship or a science ship to carry terraforming equipment is probably a hundred times more expensive than getting an actual freighter to do the exact same thing. One is responding to emergencies, as has been mentioned. If you're doing disaster relief, just being able to send medical supplies and aid isn't enough, you need a ship with shuttles, transporters, powerful sensors, trained crew, and all the other amenities to distribute these supplies. This is why warships are highly useful when responding to natural disasters and providing humanitarian aid today.
The other and perhaps more central one is power projection. If you're trying to send ground forces to a planet, or build a base in contested territory, or lay mines, or defuse a minefield, or really do anything of consequence that another power does not want you to do, it's going to involve moving material around, then doing stuff with that material, whilst under threat of enemy action. A freighter can haul lots of equipment, but how is a freighter going to cope when a Romulan Bird-of-Prey materialises in its aft quarter when it is midway through deploying an automated listening post? A freighter might be able to transport a team of Starfleet negotiators to a border world beset by pirates, but what will it do when the pirate ship shows up?
The last are a broad range of jobs probably more analogous to what we use quite specialised ships to do today. If you want a ship that can transport and deploy an oil rig, for example, or send a polar research team to the Antarctic with all the supplies they need for a six month duration stay including polar exploration vehicles, modular habitats and a helicopter, or lay undersea cables for a new high speed internet connection, you can't send a commercial freighter. A Federation starship can do all of these jobs at once, whilst also protecting itself from pirates and other hostile forces, and having a crew of capable experts who can come up with on-the-fly solutions to almost any emergency or unforeseen event.
The only thing I would hasten to add here is to argue against the suggestion that scientific facilities are not important for doing these jobs. I think they are, because what it gives a starship is the ability to flexibly respond to many different situations. Powerful sensors and an astrogation suite full of nerds means that you can track pirates who've made the mistake of thinking they can hide in a nebula. Having your own botanists means that you can come up with a solution for the space-kuzdu which is threatening to eat a fledgling colony, and having a diplomat on board comes in handy for negotiating a trade agreement when your xenologists realise the kuzdu is sentient.
Fundamentally, Starfleet seems to operate on a "cruiser" doctrine based around highly capable ships capable of responding to many different contingencies, operating shuttles and supporting other vessels, and doing basic research. Even the
Defiant class, often considered by fans to be a "pure" warship, has a shuttle bay, internal cargo space, is seen
doing a scientific survey in one episode, had its own science labs and was able to launch probes into a subspace anomaly. If this seems idealistic, consider that sensors which can probe anomalies are also useful for reconnaissance, and labs which can do sample analysis can also be useful when you're doing
a cheeky bit of war crimes with the lads to intimidate the Maquis.