The acceleration of gravity being 9.81 m/s2 is a sort of average value for the surface, having been measured experimentally in various places and calculated using the fine details of the Earth's shape and composition. We don't have to get into those details, but we can get pretty close to it using a simplification about the Earth and Newton's law of universal gravitation.
The assumption we will make about the Earth is that it is exactly a sphere with a constant mass density (its mass is evenly distributed throughout its volume). The gravitational force exerted by a so-called uniform sphere on any object outside of it turns out the be the same force that would be exerted if all the mass of the sphere were located at its center.
Therefore, an object of mass m on the surface of a uniform sphere of mass M and radius R would feel a force of gravity from the sphere equal to the force it would feel from a point mass M located at the center of the sphere, a distance R away from the surface. The force of the sphere on the object can then be written with Newton's law of gravitation as:
Fg = (GmM)/R2
which we can rearrange to:
Fg = m(GM/R2)
which is useful, because the term in parentheses never changes for a given uniform sphere, no matter what object is placed on the surface. For a given sphere, that quantity in parentheses is a constant, which we can call g. Then we have:
Fg = mg (for an object of mass m anywhere on the surface of the sphere), with g = (GM/R2)
Because Newton's 2nd law of motion tells us that F = ma, that means g must be an acceleration, the acceleration of gravity. If we know that mass M and radius R of the sphere, and the universal gravitational constant G (which can be easily looked up at this point), we can calculate g for the sphere.
If you put in the standard values of M and R for Earth (which you can also quickly look up online), you will get g ≅ 9.83. This is not quite 9.81, because the Earth is not a uniform sphere. It is actually squashed a bit from a spherical shape due to its rotation, and does not have its interior mass evenly distributed throughout its volume. In addition, its surface has all kinds of bumps and dimples on it, so that it is not even a smoothly curved surface. Because of all these differences from the "ideal" shape, the value for g is not exactly the same at every point, and so we calculate an "average" value, using more careful measurements and observations.
But this idealized calculation still gets you reasonably close. I hope this helps out! And let me know if you have any other questions about this or similar topics.