Doesn't gravity attract objects in space until they collide?
When an object is in orbit, there are two factors at play, not just one. The first, as you mention, is the force of gravity pulling the objects together. However, each object also has a momentum component which is generally (in the case of circular orbits) perpendicular to the direction of the gravity.
If we look at the common situation of a small-mass object orbiting a large (massive) object, then we can ignore the perpendicular velocity (momentum) component of the larger object and reach a simplification: The smaller object is continually pulled towards the primary but perpetually 'misses' due to its own perpendicular momentum.
I think it's worth mentioning that, given enough time and accounting for entropy, even the most stable of orbits will break down and either throw a body out of orbit or the two will collide. That said, this takes billions of years in orbits as stable as our own without something catastrophic, like a rogue planet or black hole interfering.