Why and how does lung collapse?

A few things. There can be multiple causes of lung collapse. The can leak air like a flat tire. This is called a pneumothorax. The airways to different parts of the lung can be plugged causing collapse. This would be pneumonia or atelectasis.
A lung leaks air. A collapsed lung (pneumothorax) is due to an air leak from the inside of the lung through the covering of the lung, out into the space between the lung and the ribs. The site of the leak has some damage, which will heal itself. The leaked air stays inside the rib cage and takes up space, preventing the lung from fully inflating (the lung stays partly "collapsed").