This is usually caused by either a broken fan or dust, or both because of dust. Almost always it's dust, unless age kicks in.
If the computer is quite new (between today and 2 years) check if the fan is working correctly or not while it's on. DO NOT take it off and check it; it'll damage the CPU and probably make it into a burnt silicon, quite like a burnt skin. If it rattles it's probably because the fan isn't attatched properly. Attatch it back.
If the computer is moderately old (between 3~4 years) check for any dust in the computer. Blow them away with compressed air or just use a straw and blow them yourself. Get rid of all the dust in the case.
If the computer is really old (+5 years) and if the fan is not spinning quickly then replace it. Or better, get a new computer
For note, hard drives rattle after a while when you're placed in the case wrongly; make sure you screw them on tight, and the case is heavy and sturdy that it doesn't shake, or more like can't shake. Every little vibration will in the end count for something, and damage the hard drive sorely.
If you think it doesn't do anything to the hard drive, wait few more years when it dies on you.