It has to do with creating order within the chaos that heavy emotions create. When we are feeling happy and content there is no reason to create order - the order is already established. Moreover, if there is some kind of chaos in feeling happy and well, we gladly accept it.
Whilst while feeling bad, we search for meaning, and we try to ease ourselves and ease the chaos around us and (most importantly) within us. Writing comes in very handy. It orders your thoughts, and with it brings some kind of relief. By ordering our thoughts on the paper/the screen we are able to see them, externalize them, manipulate them. That brings sense of control.
Ok I see now I went to far, this should be an article on its own. Thank you for the inspiration. Great read!