Piety tells you more or less, how religious a person is. With certain traits (zealous or humble), you will gain extra piety each month, similar to how you gain monthly prestige through having lots of land. Certain random event options will cost you a certain amount of piety, because these actions may be considered unhonorable.
Besides the monthly gain (or loss) from having certain traits, Piety can be gained by fighting religious wars. Piety is gained from winning religious wars and occupying heathen or infidel provinces. However the greatest source of direct piety gain comes from battle. Winning large battles against religious enemies will earn you and the generals who lead the army a great deal of piety.
You gain a relationship bonus (or penalty) to all religious type characters based on your piety in the same way that prestige boosts your relations with feudal characters. This bonus caps out at +20.
There are some important actions in the game that require you to have piety to spend before you can do them. This includes divorce (100 piety) hiring a holy order (150 piety) or requesting an invasion CB from the pope (500 piety). Becoming Catholic Christian after being a heretic also costs piety, around 500.