I know how castles were used. What I don't understand is given the amount of time it takes to construct one (the stone version during the medieval period) how was it possible to plant one on your opponent's land without giving him the time to raise an army and interrupt the construction?
Castles were primarily defensive, but they could offer offensive advantages. Fear of these offensive advantages was one of the reasons why the English besieged Mont St. Michel in Normandy so frequently (though unsuccessfully) in the 100 years war.
Mt. St. Michel was deep behind English lines. It was held by the French pretty much throughout the wars. It's small garrison was not too worrisome, but, if the French sent a strong force by sea, they would have a secure beach head at Mt. St. Michel where they could land and then launch devastating raids into English held country.
So, a castle offered a potential base for offensive operations if it was in the right place.
However, for castles to be built as part of an offensive strategy was more difficult. Really this required your army to be strong enough to hold the countryside while you were building castles in advanced locations which could be used as bases for further offensive operations. One place where this happened was in Iberia during the reconquista. Another place was in the Baltic, where the Teutonic knights built castles which could serve as bases for further advances against the pagans.