For example, books I'm reading would mention the 'County of X' or 'Duke of Y', but where did these counties or duchies established?
These entities came into existance as we know today after the dissolution of the authority of late Carolingian emperors during the IX century.
Counts were royal officials drawn from landed aristocracy to act as the king's representatives in a region he couldn't rule on his own. He had the roles of judicial and military governor, other than collecting taxes. He didn't own the lands he was charged with governing: those belonged to the king, which permitted him to use some of their wealth to perform his duties. If the king felt he was needed elsewhere, (or he revolted) he would replace him with someone else. The ruler would also reward the oath of loyalty pledged by said person by giving him wealth, mostly lands, as a beneficium ("benefit"), creating a vassal, basis of the vassallatic system.
The word "count" comes from Latin comes, used in Classical times to describe the assistants of a magistrate ruling a province, and later on from Constatine's (274-337) times, started to describe high ranking officials. By association, it also meant "people of a retinue", more prominent with this meaning during the later years after the end of the Western empire. Since counts were drawn from close members of the king's clientele (his vassals), it was applied for this role as well.
After the civil wars following Charlemagne's death, there must have been a tendency to pass from father to son both the role and possessions of count. Maybe as an unchecked succession or by political pressure, we are not sure. What we know is that in 877 emperor Charles the Bald decreed in the Quierzy Capitulary that royal vassals could pass unto their sons the role and beneficia of count. This resulted in a gradual welding of the role of count with the ownership of the lands the count was administrating, effectively creating dynasties of counts and marquises (marchs were border territories of mostly military nature following the same logic). During the X and XI centuries, counts began subjugating other territories belonging to other counties, overstepping their bounds as did other landowners which weren't vassals or counts, but simply influenced with force an area surrounding their own possessions. This was called "bannal lordship" by some historians, and is a justification of the presence of some persons simply named "X lord of Y", without titles or affixes.
Dukes have a slightly different origin. The Latin verb ducere means "to lead". A dux was a military commander of one or two provinces during the late Empire. It was also used to describe some rulers or commanders which lead barbarian migrations during the IV and V centuries. This supports the hypothesis of an ethnical connotation of the title, since earlier dukes were powerful nobles ruling an area more or less ethnically and politically homogenous (for example, Tassilo duke of Bavary during Charlemagne's times). Langobard poltities in the Italian peninsula were divided in duchies, as they were organized possibly in a federation of tribes divided in different migrating groups, named farae (sharing origin with the German verb fahren), almost with no usage of the term or concept of count. Similarly, Byzantine military governors of Italian provinces were also named doux, Greek term based on the Latin one.
Throughout the centuries, these territories became an hybrid concept of a private possession connected to an adminstrative unit or perceived area, as they could be used to denote borders and areas where some laws and customs applied rather than others, but this subject is very ample and still debated.
The hierarchy of baron -> count -> duke -> prince -> king is a modern invention, most likely originating in the late 1600s-1700s when these titles started losing benefits and privileges and where simple honorifics. Realistically, they denoted different types of polities and relative importance of the holdings. For example, around 1052, in Southern Italy we would have a prince of Capua, a duke of Benevento, a count of Aversa and a count of Teano, everyone within 50 km from one another.