From the appearance of organized castle production in Europe during 9th and 10th century AD, castles were made on numerous locations that served integral purpose to the central function of those castles. From military keeps, royal summer houses to countryside centers that regulated crop production and storing, numerous castles were made across all terrains Europe. Here you can learn more about how those locations were picked.
Easiest way of providing the best natural protection for castle was to build it atop a hill, but that caused many other secondary problems. All the road infrastructure had to be made usually from scratch, construction was very expensive, and once besieged, castle inhabitants had no way of reliably receiving resources (mostly running water). However, many castles were created on hills simply because geographic landscape disallowed low-ground construction. For example, 66% of Germany is in highlands, and majority of France is in lowland. High position of castle was also chosen for better visibility, which made it a symbol of power.
Many castles were created at or near roads that were created by the Romans. When built away from that infrastructure, castle lords had the ability to create their own road structure.
Before age of castles, many fortified structures were created and destroyed on the best possible defensive locations all around the Europe. Many castles were built just on those locations (abandoned Roman bases, Oppida hills and more).
Sometimes, fortified keep was a intentionally constructed to serve as a starting point for a creation of new city or network of villages. Its protection, construction of roads and local government housing enabled many regions to thrive and grow. In some cases, castles were also inserted into pre-existing communities to control and subdue the populace, or to protect them against foreign threats (invasion of Normans into England). This sometimes caused destruction of some civilian houses or farming lands where castles were chosen to be constructed.
As castle construction became more and more common across Europe, nobility and royalty started looking them as status symbols that need to be as extravagant and expensive as possible. Because of that, many castles were created for sole purpose to serve as summer houses in the vicinity of hunting forests or fish ponds, all requiring significant funds for their upkeep. There were also examples of impressive looking defensive castles that were created just to be pretty, with water motes that were shallow, and castle battlements that were impractical.