The two most important things to keep in mind is
that one, sun and earth both are not stationary masses.
Two, there are gravitational forces from other planets affecting earth.
Of course earth may collide into sun if both are stationary- imagine pulling your friend towards you while he is standing still, he'll directly hit you right?
Now if he is running towards you with one arm stretched and you pull that arm towards you, he'll probably spin around you isn't it?
That's what is happening in solar system too.Taking into account the high speed motion and other forces acting in the planets they don't collide.
You'll study about concepts like linear, angular momentum and centrifugal forces in higher classes.