why doesnt sun attract earth towards it

It does. Were it not for the attraction, the earth would go spinning into the wild blue yonder. (Of course, the universe is expanding, anyway.)