A castle is a fortified personal residence.

There would be many fortified structures on the landscape, at varying levels of expense and sophistication, but most would not be castles...they'd be forts, fortresses, stockades, palisades, walled cities, etc.

The powerful would also have unfortified luxury residences--palaces and manors.

So a good way to think about where castles would be is to consider the history of the setting and imagine the socio-political expedients that could factor in: where do rulers need to be to maintain their power, cross-indexed with what places are critical to military expedients (must be controlled and defended) and socio-economic expedients (must be ruled closely because of their importance to trade and diplomacy)...and both embedded in historical precedents of where past conflict and nation-building required regional control to be both militarized and to have a local ruler on-site (who does the king need to have a castle and why).

If a family has had power in a region for a very long time, generation after generation of fortification of their traditional home base might result in a castle. Japanese castles are a great example of this.

Castles are both defensive structures and political statements, so they tend to be placed at locations that are defensible, but also situated within or adjacent to critical urban areas and trades thoroughfares (roads, waterways, ports). On one hand, the castle is then part of the defense of the critical region, but also an stamp of ownership. The castles of Prague are a great representation of this.

Since castles are very expensive, one way to lower the cost of creating one is to simply take an existing fortification and modify/improve it, so the castle's position reflects strategy decisions of some past conflict that may or may not be relevant. The Red Fort and Lahore Fort are both examples of this.

It also notable that offensive fortresses--built to impose control and act as central depots for invaders/occupiers--are often available to be converted into castles as outright warfare gives way to occupation: the new political leaders need both fortified defense and to squat on critical strategic points. Edward I castle's in Wales are examples of this.

On a national scale, there's also a bunch of factors that play out over time, amounting to: who is allowed to have how much of a castle, weighing regional stability and internal intrigue versus the priorities and budget set by central rulership. It's a part of the military budget: where to spend the most money on fortification and maintenance of fortification, and where can things be allowed to be less fortified or less maintained, to the greatest benefit of the center.

If you're the central ruler--king or parliament or whatever--then you have to do a bunch of calculations about who is allowed to have how much military power out in the distal regions. Handing someone a fortification to control is an act of trust, handing someone a fortified residence that also has critical strategic/trade value is incredibly risky. A distal power--a duke or daimyo, etc--builidng or adding fortifications under their own discretion is possibly signallling that they're going to secede or revolt, so often there are explicit rules about who can build how much fortification that go along the rules about who controls how many armed men. While the best examples how dangerous dukes are all War of the Roses, the best examples of the internal politics of castles are all the rules imposed by the Tokugawa on the daimyo--the Edo bakufu not only setting explicit rules on the numbers and types of fortifications and castles the regional rulers were permitted, but spying on them to ensure compliance.

Castles as military structures also factor into maintaining borders and expanding borders of the nation--again, Wales being the cleanest example--so generally the best budgeted and newest fortifications are along borders with high tension, and borders that have expanded recently. Internal regions that are seen as stable--neither going to rebel and not likely to be invaded--can spend less on castles-as-fortifications, so the existing "castles" may have out-of-date defenses and not really function as a strategic launch point any more....but still be maintained as luxurious residences for the politically powerful that require personal security.

In extremis...since castles are very expensive the ones that no longer have a pivotal strategic purposes, or the ones that were built in service of a rulership faction that are now eliminated or stripped of power, would be abandoned or deliberately pulled down.

I would argue for purposes of that last question--where would there be abandonned castles--that fortresses and forts should be considered in addition to castles, because as purely-strategic constructions they're much more likely to let go because their military purpose has expired; they have no political-status value such that they'd persist as a posh residence; and they're more likely to be sited in some distant, isolated location that only became important as a zone of control during a past conflict.