I'm staying over in New York for 4 hours before my next flight that lands again in America. If I do my immigration at New York, can I leave the airport for example to get food, and then just come back 3 hours later and wait for my next flight? Or do I need to wait in the airport until my final flight?
Yes, you will pass through immigration and customs at your first point of entry when your plane lands in New York. At that point, you have entered the US and you may go wherever you like.
You will probably find, however, that 4 hours isn't that long for a domestic connection. The New York airports are always busy, and you may be waiting for some time to pass through the border checkpoint. Then getting anywhere from the airport takes time, no matter how you choose to travel. You will definitely not have 3 hours spare to wander around.
There are plenty of options for food inside the airport for people in exactly the same situation as you.
In theory, yes, though you may not have enough time.
if I do my immigration at New York
You have no choice about that; you will do immigration there. You'll also need to claim your checked bags, if any, clear customs, and recheck the bags, after which you'll need to pass through TSA security before reaching your gate.
All of this could easily take a couple of hours. It's unlikely to take much less than one. Even reaching the gate to get off the plane can take 30 or 45 minutes at JFK; such times are not at all unusual, and if something exceptional is happening then it can be longer.
Once you've rechecked your bag, nothing keeps you from leaving the airport, but do check the security line to see how long it is before you make any decisions. It takes an hour or more to get to Manhattan, so that's almost certain to be out of the question. If you're arriving at JFK, you might take the A train to the Rockaways and have a stroll on the beach. If you're arriving at Newark, there's not much to do as far as I'm aware.
You will be able to leave and return again, but bear in mind, you will need to clear security again. You will not however, need to clear customs and immigration. You do that at your first port of entry.
As the other answers say, you must go through immigration at your point of entry and you can leave the airport in principle. However, there's nowhere to go that's close enough to the airport that you could get there, do something, and get back to the airport in time to catch your connecting flight.
You can leave the airport, but four hours is not that much time, especially as you will need to go through immigration and security (for your connecting flight) in that time. Also, bear in mind that you will need to obtain a visa before flying to New York, unless you qualify for a wavier (I don't know the conditions on this, but all my colleagues from the UK here need to have a visa just to pass through the US between the UK and Mexico).