The decorator pattern allows adding new functionality to an individual object dynamically at runtime without affecting other instances of the same class. It involves wrapping the original class with concrete decorator classes that contain the additional responsibilities. In the example, a window object is decorated with scroll bar functionality by nesting decorator classes like VerticalScrollBarDecorator and HorizontalScrollBarDecorator that extend an abstract WindowDecorator class and wrap the decorated window object. This allows constructing windows with any combination of scroll bars without subclassing the window for each possibility.