To demonstrate how does a capacitor work, let us consider a most basic structure of a capacitor. It is made of two parallel conducting plates separated by a dielectric that is parallel plate capacitor. When we connect a battery (DC Voltage Source) across the capacitor, one plate (plate-I) gets attached to the positive end, and another plate (plate-II) to the negative end of the battery. Now, the potential of that battery is applied across that capacitor. At that situation, plate-I is in positive potency with respect to the plate-II. At steady state condition, the current from the battery tries to flow through this capacitor from its positive plate (plate-I) to negative plate (plate-II) but cannot flow due to the separation of these plates with an insulating material.
An electric field appears across the capacitor. As time goes on, positive plate (plate I) will accumulate positive charge from the battery, and negative plate (plate II) will accumulate negative charge from the battery. After a certain time, the capacitor holds maximum amount of charge as per its capacitance with respect to this voltage. This time span is called charging time of this capacitor.
After removing this battery from this capacitor, these two plates hold positive and negative charge for a certain time. Thus this capacitor acts as a source electrical energy.
If two ends (plate I and plate II) are connected to a load, a current will flow through this load from plate-I to plate-II until all charges get vanished from both plates. This time span is known as discharging time of the capacitor.
Capacitor in a DC Circuit
Suppose a capacitor is connected across a battery through a switch.
When the switch is ON, i.e., at t = +0, a current will start flowing through this capacitor. After a certain time (i.e. charging time) capacitor never allow current to flow through it further. It is because of the maximum charges is accumulated on both plates and capacitor acts as a source which has a positive end connected to the positive end of the battery and has a negative end connected to the negative end of the battery with the same potency.
Due to zero potential difference between battery and capacitor, no current will flow through it. So, it can be said that initially a capacitor is short-circuited and finally open circuited when it gets connected across a battery or DC source.
Capacitor in an AC Circuit
Suppose a capacitor is connected across an AC source. Consider, at a certain moment of positive half of this alternating voltage, plate-I gets positive polarity and plate-II negative polarity. Just at that moment, plate-I accumulates positive charge and plate-II accumulates negative charge.
But at the negative half of this applied AC voltage, plate-I gets a negative charge and plate-II positive charge. There is no flow of electrons between these two plates due to dielectric placed between the plates but they change their polarity with the change of source polarity. The capacitor plates get charged and discharged alternatively by the AC.