Doesn't gravity attract objects in space until they collide? If the formula to calculate the force of gravity between two objects is:

$$F = GM_1M_2/r^2,$$

why do planets stay in orbit? Or is there another formula at work?
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.