Phasers and torpedoes are, as was mentioned, much like deck guns and torpedoes in WWII-era naval warfare. The idea of the Klingon and Romulan warbirds being able to cloak and thus be hidden was a direct analog to the German U-boats.
Tactically speaking, you want as many types of weapons as you can get when you're in a fight. This applies virtually across the board, in all situations. If one weapon doesn't suit the situation, you bring another. It's why police officers don't just have guns, but batons, tasers and pepper spray as well.
Back in-universe, phasers are the go-to weapon. Phasers are energy-based, meaning as long as you have sufficient power you can continue to fire them. They are very powerful against most unshielded targets, and can be brought to bear from nearly 360 degrees (provided all phaser banks are operating nominally). Lastly, being a beam, they can usually be aimed and fired pretty quickly at a target at almost any renge at which the weapon is effective (even right up close). However, depending on the race's level of technological advancement, phasers often have minimal effect on shields or even ship hulls. They are also limited by the speed of light, so they cannot be used while travelling at warp.
Torpedoes are the Star Trek equivalent to the same weapons in naval combat, or to anti-ship missiles like the Harpoon or Exocet. Get hit with one of these with your shields down and your ship has a big gaping hole in it. The explosion also requires a lot of shield power to repel and can cause damage even with shields up. They also require minimal power to fire, making them the principal weapon of cloaked ships like the Klingon Bird of Prey, and they can be used while the ship is travelling at warp, as they maintain a "warp bubble" generated by the firing ship. However, they are an expendable weapon; it is implied that torpedoes are too complex a machine to replicate, even without their antimatter payload. They are also, for the most-part, non-seeking; they must be given their initial guidance from the firing ship, and if the "sight picture" changes (the ship radically changes course or speed) the torpedo can miss. Torps have a maximum rate of turn and come out of the launcher along the fore-to-aft axis of most ships in the universe (there are a few turreted launchers), meaning their effectiveness in a "broadside" confrontation is reduced. This makes torpedoes a weapon you use in head-on combat when you are pretty confident that you will hit your target, usually meaning relatively close range.