Squeaks are caused when the subfloor begins to separate from the floor joists. The nails squeak as they slide in and out of the joists.
To fix squeaks caused by large gaps from beneath the floor, fasten a piece of scrap wood against the floor joist so that it fits snugly against the subfloor. The wood will support the subfloor, preventing it from moving down to the joist.
For smaller gaps, apply construction adhesive to a wooden shim, and slide the shim into the gap between the joist and the subfloor. Don't force the shim into place because it will create a wider gap between the joist and the subfloor.
Fix squeaks from above the floor with a hammer and finish nails. For wood floors, locate the squeak, and drive a long finish nail into the floor so that it goes into the floor joist if possible (locate the floor joists with a stud sensor). If the problem is caused by the floor's separating from the subfloor, drive two nails into the floor at opposite 45-degree angles. Use a nail set to drive the nails below the surface of the floor, and fill the hole with wood filler.
For carpeted floors, cut a small hole in the carpet webbing above the squeak with a utility knife. Peel the carpet back, and drive a screw through the floor and subfloor. The hole should be large enough for the head of the screw to fit through. If the head starts pulling down the carpet, loosen the screw, and pull the carpet over the head. Increase the size of the slit if necessary.