Zap! You just touched a metal doorknob after shuffling your rubber-soled feet across the carpet. Yipes! You've been struck by lightning! Well, not really, but it's the same idea.
Your rubber-soled shoes picked up stray electrons from the carpet. Those electrons built up on your shoes giving them a static charge. (Static means not moving.) Static charges are always "looking" for the first opportunity to "escape," or discharge. Your contact with a metal doorknob—or car handle or anything that conducts electricity—presents that opportunity and the excess electrons jump at the chance.