Unit match ups are a key area in StarCraft 2. So much that a game that I have played has been decided on by my failure to properly counter a unit the other person is producing. To be able to do this you need to first scout your opponents base and determine what unit structure or composition they are producing. For a Protoss player like myself I usually scout the opponent right after i put down my first Gateway at 13 supply. It is important to keep your scouting worker alive in the opponents base for as long as possible in order to gain as much knowledge about their build and unit production as possible.
For example (Mostly Protoss examples):
I scout a Terran opponent and I see with my scout that he already has a barracks with a techlab attachment, then i know he is going to try an early Reaper harass so i get my cybercore up and chronoboost out a Stalker (Stalker beats Reapers).
To conclude my thoughts,
It is important to scout your opponent sufficiently in order to sufficiently counter their attacks and ultimately push into their base and win!