Shouldn't black holes exert the same gravitational force as an object of similar mass but lower density? If the earth got shrunk into the size of a peanut, it would turn into a black hole, which would have a higher density but same mass. Since the center of mass of both bodies would be the same, the distance between a far-away object and the centers of mass would be the same. Since both the variables (mass, distance) would be the same, wouldn't the gravitational force exerted by both the earth and the black hole on a far away object be the same?
If this is true, wouldn't light be unable to escape the earth as well, since light can't escape black holes?
 A: It is the case that for large distances the gravitational field of Earth and its black hole equivalent are the same. The same with any other object. For example, if you replace the sun with a black hole with the mass of the sun, almost every planet in the solar system will continue to move in the same orbit (but of course temperature will drop significantly in each planet).
This situations can already be explained at the level of classical physics. It is proven by Newton's shell theorem that the gravitational field of a point particle of mass $m$ and a homogenous sphere of mass $m$ have the same gravitational field at distances larger than the radius of the sphere.
As for why light can escape Earth and not the black hole with the same mass, it is related to the force being stronger as the two objects under consideration (light and Earth/BH) are closer.
For a tiny particle trying to escape the surface of Earth, the gravitational field is already reduced by 1/$R_\oplus$ where $R_\oplus$ is the radius of Earth. Starting inside Earth does not help, as by the same shell theorem we just consider the gravitational field given by the mass below, and as you go deeper, there is less mass and a weaker gravitational field (thus the particle may still escape). For example, there is no gravitational field inside a hollow sphere with the mass of the Earth.
For a tiny particle trying to escape a planet with the same mass as Earth but with the radius of peanut $r\approx 5\;\mathrm{cm}$, feels a gravitational field that is $R_\oplus/r = 10^8$ stronger and may not be able to escape.
The calculations are slightly more complicated if you consider general relativity (GR), photons and black holes, but the same conclusions apply. For a generalization of the shell theorem in GR see Birkhoff's theorem.
A: The parameter you're not considering is the distance.
The Earth is an object with the mass of the Earth $m_E$ and the radius of the Earth $r_E$ (duh). If you take a black hole with mass $m_E$, then its radius will be the radius of a peanut, $r_p$.
When shooting a light ray on Earth, the light easily escapes its gravitational pull because it is shot at a distance $r_E$ from the center of mass of the object. When shooting a light ray near our black hole, it can't escape its gravitational pull and falls into it, because it is shot from a much shorter distance, $r_p$. If you shoot a light ray at a distance of $r_E$ from the black hole, it will behave the same way as it does on the Earth. This is the punchline: light can escape black holes from a great enough distance.
Then does this mean that if we go near the center of the Earth and shoot a light ray when we're at a distance of about $r_p$ the light will be pulled in the center of the Earth? Of course not, because the mass "inside" a radius $r_p$ of the Earth's center is much much smaller than $m_E$.
A: I am posting this answer here because the duplicate question that I was originally
Writing for was closed as duplicate:
Why light can't escape a black hole but can escape a star with same mass?
For the purpose of this question as posed I will use a neutron star before and after collapse.
There are really nice answers (I am referring to the original question) by @nielsnielsen and @Jerryschirmer, I feel like I need to add this answer because the explanations though perfectly correct, do not explain some very important points about gravity being very special:

*

*Gravity creates more gravity and is always attractive, this can be said in many ways, like gravity is self interacting or gravitons emit gravitons or that curvature possesses energy and thus creates more energy, but the important thing to understand is that gravity is able to “build” large objects where (like in a neutron star) after a certain limit gravity can overcome the other forces and become dominant, crushing the object and compacting it into a smaller and smaller region of space while the effects of gravity can grow limitless.


*The distance from the object does matter but in a non-trivial way. You are saying that the gravity of the star before and after collapse is the same but this is not completely correct. What is correct is that far away from the objects the effects of gravity can be felt the same way regardless before and after the collapse. But as you get closer to the objects this becomes more complicated and the answer is the (qualitative and quantitative difference in) structure of spacetime around the two objects and how their mass (in reality stress-energy content) is allocated in space.
Very naively saying, at the surface of the neutron star the full energy content of the star is affecting you in an attractive way towards the center of mass, and hovering at the EH of the black hole, same is true, and assuming same energy content, we should feel the same effects. But as you see from the answers, the escape velocity (which is just one way of expressing the effects of gravity) does depend on the compactness of the object. But why? I believe this question is asking deeper into this explanation and the answer lies in the description of gravity as the only known force being able to overcome the other forces and compress the energy content into ever smaller volumes of space. This, together with the fact that gravity is self interacting (always attractive) and that spacetime curvature itself has energy and thus creates more curvature gives you the real answer what happens when gravity compresses the energy content of the neutron star into and ever smaller region. The qualitative difference lies in the fact that in the case of the neutron star the energy content (and curvature) is spread over a larger volume of space combined with the fact that gravity is self interacting. Imagine all the particles in the star curving spacetime around themselves, but are spread out in space and (the effects of curvature) cannot overlap and constructively interfere (self interact) as powerfully as they do in case of the black hole. If the energy content is compressed(in the black hole) the particles simply get closer, increasing the self interaction of gravity, and this effect can grow limitless and leads to an event horizon.
Remember distance matters when dealing with gravity but not only for large objects, but for elementary particles that make them up too, if the particles are closer, their gravitational effects are stronger (relative to when they are spread out) and can create extreme spacetime structures. This is the ultimate answer to your question. This can create the extreme effect of an event horizon.
