earth and moon have there gravity by which they attracts heavy body so why they donot come closer and attach to each other?
The moon does not collide with the Earth because of the fact that it has velocity that is nearly perpendicular to the pull from the Earth. So instead of accelerating the moon toward us along the line that joins the earth and the moon, the gravitational force just changes the direction of the moon’s velocity vector to be tangential to the circle of its orbit.
It is the same reason that the Earth does not pull Mars into collision, because if you remember from Newton’s law of universal gravitation, everything exerts a gravitational force! But because of the object’s tangential velocity, it keeps moving in a circle.