[This post is part of a larger strategy guide for Civilization 6]
In my mind, there are essentially only two strategic game states: ramping, and war.
Ramping is when your primary strategic goals are focused on building infrastructure and growing your economy. The word "ramping" comes from the idea that as you invest in infrastructure, you generate more tools to generate more infrastructure more quickly. Ramping is all about compound effects. All things being equal, you should aim to be in a ramping state as much as possible. And mechanically, you should aim to get to the point where, if you're on your own continent or landmass, you'll win just from being able to ramp more than everyone else.
War is war. To be more specific, war is when your primary strategic goals are to defend yourself, to prevent someone else from ramping, or to jump start your own ramping by taking resources from someone else. By default, war is a cost. Every turn that you are building units is a turn that you are falling behind on infrastructure. Every coin that you spend on unit maintenance is coin that wasn't spent on infrastructure. This may be counter intuitive, but war is not really about eliminating other people; rather, it's about making sure the rewards from war are higher than the opportunity cost of your units. You might win a long and drawn out war, but it may not be worth it if you primarily end up with low population cities and no additional infrastructure, while other opponents have been ramping up unopposed.
For the most part, you'll find yourself in some mix of these two modes — you'll be making a few units, while also building up infrastructure.
My one piece of strategic advice for this section: don't do that.
At a high level, the game is set up to encourage going all in on one mode or the other. This is primarily backed by policy cards and governments — for example, Agoge, which cuts 50% on training time for warriors, slingers, and archers is a must have for building early units and incentivizes war preparation in all of your cities. Over the long term, the structure of the tech tree encourages very different progression depending on what mode you're in. Going for the next unit upgrade (warrior → swordsman, archer → crossbow) makes a ton of sense for war, but may delay unlocking commercial hubs and industrial zones and significantly set back your ability to ramp. Overall, when you decide to go to war, you should aim to be quick and decisive, with a clear end goal in mind.

This all begs two questions: when should you go to war? What justifies switching off a ramp state?
Here are some general things I look for when deciding if I should go to war:
Can I shore up a defensive position? Sometimes a city state or an enemy will have a city at a natural chokepoint — maybe a one-tile mountain pass, or a canal city that leads into your general territory. Going to war to take that city is often really worthwhile, because you will be offsetting the likelihood of having to defend against wars in the future.
Do I have a lot of units just sitting around? Sometimes, you go to war, and then you win. And then you have a bunch of units sitting around. If you are near city states or opponents, sending in some units to just pillage things will often give you a boost while forcing your opponent to stop ramping (at least for a bit). Note: I am not saying that you should just throw units into a meat grinder. But if you think you can get away with grabbing a city or pillaging some tiles without having to make more units, this will give you 'free' (already paid-for) resources.
Am I behind? I'm losing the game. I'm behind on science and culture per turn. Even if I ramp as best as I can, I know my opponent can out ramp me — maybe they have better tiles, or better synergies with their civ, or they're just better at optimization than I am. In this setting, I pretty much always start preparing for war. Forcing a war on an opponent can slow them down and kick them out of ramp mode, giving you breathing room. Every tile you pillage or city you take has a double effect, giving you a needed boost while slowing them down. These effects are magnified in the atomic era — nuclear weapons are fairly easy to make, very hard to defend against, and can be deployed well before an opponent can move to win the game. If I'm really far behind by the medieval era, I'll scroll to the end of the tech tree and click on nukes and simply not research anything else. Then I'll bide my time, waiting to get nukes up as soon as possible.
Note: you don't necessarily have to be behind on game stats! There are a bunch of races in the game, and going to war can be a viable strategy to win any of them. Do you want to build a wonder but see that someone else will get there first? Declare war and park a unit on your opponent's wonder to prevent them from building it. Trying to get a great person but not generating enough points? Go pillage your opponent's districts so they generate fewer points too. Etc. Etc.
Am I ahead? Generally, if I'm in ramp mode, I want to stay in ramp mode. But if I'm far ahead and I know I can cripple an opponent permanently, I'll try to blitz a few strong units and take out a city or two. Just remember that defense is a lot easier than offense — you have to be pretty far ahead, or really catch an opponent by surprise, for this to work well!
At the end of the day, there is no "right" answer for when to go to war. Try to build an intuition of costs and benefits for different strategic goals — how many units do you need, how far back will they set you, how easy is it to accomplish discrete tasks, things like that. Eventually, it'll become pretty obvious when to ramp and when to war.