They don't always, but they certainly can.
The way you determine the effect on CR is to add any damage they deal to your figuring of the monster's damage per round, and to add any defensive benefit to AC/HP/or resistances/immunites to those respective traits, since those measures are how you determine CR in the first place.
So while a goblin given the ability to cast 1 fireball spell will have an increased CR because the damage of a fireball is more than a goblin can typically deal without the spell, giving a dragon the same ability to cast fireball might have no impact (or very little impact) on CR because their options for breath weapon or claw/claw/bite attacks deal similar or superior damage.
And while a spell like shield or invisibility significantly affect the creature's defensive capabilities and would affect CR as a result, spells like misty step or detect magic have basically no effect on CR due to not having a direct impact on the difficulty of defeating the creature.