Only those who die in a state of grace are in purgatory.
When you commit a mortal sin after baptism, there is eternal punishment and temporal punishment that you must pay. After confession all of your sins are forgiven and your eternal punishment (if the sins were mortal) is removed. There is still temporal punishment to pay.
An example: A lapsed Catholic murders a man. His temporal punishment is life in prison. He confesses the sin (with true repentance) and is absolved. He still must pay the temporal punishment either in prison for life or in purgatory.
We can offer our penance, sufferings, prayers, and indulgences on behalf of our sins or others’ sins to lessen purgatory.
An indulgence pays for our temporal punishments that we earn from sin that we commit. When we die, if we have not paid for all of our temporal punishments, we will pay for them in purgatory.
A partial indulgence remits part of our temporal punishment. A plenary indulgence remits all of our temporal punishment we have earned up to that point.
Note: Baptism remits all sins and temporal punishment. Confession remits only sins.
We can gain indulgences for the poor souls in purgatory and pray for them.