How does aspirin help prevent heart attacks and when should it be taken?

How does aspirin help prevent heart attacks and when should it be taken?