Static Electricity and Grounding Confusion 
*

*In an electroscope, you take metallic balls, and rub them with either a comb or a glass. Touching the balls with your finger is said to undo the effect of rubbing them with the comb or glass, since your finger is connected to ground, and ground is so enormous that it absorbs or releases the electrons to neutralize the metallic ball.

*When you rub your shoes against a carpet and then touch a metallic doorknob, a shock results, since the build-up of electrons that was built up by rubbing against the carpet is released via the doorknob to ground.
What I don't understand in #2 is how the buildup of electrons occurs in the first place. We've already seen in #1 that you are connected to ground, so in #2, why don't any excess electrons released from the carpet at the time of the rubbing immediately go back to ground? (I guess I'm asking a larger question: how can any rubbing of anything create a surplus or deficit of electrons if the thing being rubbed is connected to ground -- why doesn't that connection immediately discharge any imbalance of electrons created by the rubbing?)
 A: It's not necessary to go into lot's of details here e.g. whether it's electrons that move or ions, or if it's some energy barrier that prevents a current from flowing or the mobility of ions or whatever. All those details are completely irrelevant to the question.
All you need to look at is resistance, capacitance, charge and current.
If you are charged there is a current flowing from you to ground. The more current flows the faster you get discharged. If the resistance of your shoes + the resistance of the carpet you stand on is relatively low, a high current can flow and you get discharged in a fraction of a second. If the resistance is high it can take a while.
When you rub your feet on a carpet, the carpet and also the underside of your shoes become charged. That charge can then flow through your shoes into your body. But if the carpet has a low resistance all the charge will flow away to ground before you get charged significantly.
So to be able to receive a shock by walking over a carpet and touching a metal object, you need to have a carpet with a high resistance and also shoes that don't have too high a resistance or else the charge can not flow through the sole into you.
When you touch a charged electroscope it will mostly discharge even if the resistance between you and ground is very high. That is because your capacitance is a lot higher. Capacitance is simply the ability to store charge. The bigger an object is the more charge it can store.
So when you touch it the charge will distribute between you and the electroscope but since you are much bigger most of the charge goes to you and the electroscope is left with very little.
A: To understand this, you need to know a few things about electrons. It may help you in understanding situations like this.
Electrons Like Electrons of Opposite Spin
Yes, you read that right; electrons which have an opposite spin are attracted to each other. This is better explained using quantum mechanics, and this post is too limited to fully explain why. In any case, this opposite-spin attraction is a reason why rubbed things exchange electrons; some atoms are more "greedy" than the others, so they take on extra electrons when in close proximity to less "greedy" atoms. This is due to unpaired electrons in the valence shell attracting other electrons to fill the shell. These shells get filled in spite of the fact that it gives the whole atom a negative charge.
Electrons Like Lower Energy Levels
Electrons also like going to lower energy levels. That is, if a neighboring atom has a lower energy level available, or even if there is a lower energy available within the same atom, the electrons will try to go there. There can be, however, a cost involved for electrons switching which atom they belong to. This is called an "energy barrier," and you can think of it like a toll. If an electron doesn't have enough energy to pay the toll, it can't go to a lower energy level.
For example, an electron in situation #1 experiences being attracted to another atom (due to opposite-spin attraction). The energy barrier is effectively paid by the opposite-spin attraction. Then the electron begins feeling the force of the atoms it left (due to charge difference). The electron, however, does not have the energy to get back to the old atom. This could be because you moved the metal spheres away from the rubbed item, or the energy barrier has changed, or simply because the atom the electron is in is simply "too greedy" to let go.
Grounds Act Like Big Electron Reservoirs
Grounds are simply big electron reservoirs. If a charged item gets in contact with a ground, it transfers electrons until both things have evenly distributed their electrons, assuming no energy barriers stop them.
For example, if a metal ball is missing a mol of electrons, and you bring it into contact with a ground with a 100 billion mols of electrons, the electrons will go back and forth until the electrons are evenly distributed between the two. Even if a mol of electrons went to the charged item, it doesn't really make a difference to the ground. The charge differences get diluted enough that they are practically gone.
It should be noted, though, that when I say "contact" here, I mean that the energy barrier is small enough that the electrons (generally) have enough energy to get through. So you can be in physical contact with a rubber ball, but the electrical contact isn't there.
Back to the Two Situations
In situation number 1, you act as a ground because you have enough electrons available to transfer between the two charged things. The dilute charges (or missing charges) get spread out enough that there is no noticeable charge. The electrons don't even themselves out because, until you touch it, the energy barrier to do that is too high.
In situation number 2, you are a charge carrier, and the doorknob is your ground. The carpet has a bad enough "connection" with the ground that charges don't easily come up and neutralize unbalances until some time after you've shuffled through. More scientifically speaking, the energy barrier for the electrons that would balance out the potential difference is too high. The door knob, however, is much more electrically "connected," so you get a nice zap as the electrons scramble for a lower energy level (in the doorknob/door/earth). Once again, the energy barrier is low enough to go the doorknob, but not to the carpet/earth.
In Short: Electrons can only flow if they overcome an energy barrier. If the energy barrier is too high, they can't go. If the energy barrier is lowered, they can go.
A: Carpet is a dielectric. It means that electrons can't move in it freely. You can scrap some of them by rubbing. Because you are a conductor they can move freely on your surface. If there's plenty of electrons on you, you have huge electric potential.
When you touch doorknob which is also a conductor, electrons flow from place with big potential to the place with low potential to equal both potentials.
If you put a carpet between you and doorknob nothing would happen*. Electrons wouldn't be able to flow from you to the doorknob.
*if the potentials difference would be REALLY vast, there would an electrical breakdown occur.
Grounding is just a purely contractual term. It is just "place with zero potential". Just to refer other potentials to it.
