Their role is to provide area denial, so that the area around it is safe for other building and ships, such as frigate factories/repair platforms, and damaged capital ships and titans running from a pursuing enemy fleet. If you're TEC or Advent, the starbase can't move, and can't really be used offensively. (this situation is very common - of an Orkulus being attacked while the owner is upgrading it, and the Orky finishes upgrading just before the attacker gains the advantage over it and can kill it, and the attacker is forced to flee, having taken a good number of casualties) Usually you can time upgrades such that they become active only when you need them, to avoid wasting money, and to lure the enemy into attacking, thinking they can win, but then the starbase upgrades and they lose. Only build them as needed, since they're pretty expensive.
If you're Vasari, your starbase is your most important weapon throughout the whole game, so you should aim to starbase the most important gravity wells - the colonizable choke points, preferably.