The body is complex and when the heart is broken the body starts to fall apart little by little. The underlying pain can be very clear or buried deep in your heart. There are many professional who can help you tackle this pain and many ways to do emotional clearing. Although I am not a huge fan of sitting on a couch and talking through your problems that can help as well.
Your body will protect itself from pain. All types of pain emotional and physical. You have heard of the person who had a childhood trauma and has blocked those events. Those events will still effect the way the person sees them self and the world, even if they do not remember the events in their conscious mind. Think of it this way the brain protects us from the memory, but most times the protection is not handled in the correct way. This is not just the case with blocked memories. Your subconscious can interperet events in strange ways.
Looking at weight specifically you body will protect itself from a becoming something you perceive to be bad. I am going to give you an example and this may not pertain directly to you but you will get the idea. If as a child your parents got a divorce because your father cheated with a women you perceived to be skinnier and prettier than your mom. As an adult you may see being pretty and skinny as a bad thing and not even know why. Your body will protect you and not let you be skinny so you will not be a home wrecker. Going the opposite way if you perceived that women to be uglier and fatter -- you may think if you are skinny and pretty you will be left. These are straight forward examples, but more likely it will not be that cut and dry. Also these attitudes usally affect more than just your weight. Your subconscious interpretation of an event is determined by your state of mind at that event.
Self sabotage is a well know thing that happens with emotional pain. The feeling of not being worthy. If you have hurt someone in the past you may sabotage your happiness or health to sacrifice yourself. Most of the time the pain is about not forgiving yourself vs actual pain you caused. Other time you punish yourself for something that was out of your control. This is seen in abused children, widows and divorces but can also be seen in Christians who have changed there life around and cannot grasp God's all forgiving love. When we do not feel like we are worthy of being happy our body helps us out and does not allow us to achieve health and happiness.
Hiding, if we pack on the pounds we can hide behind them and not be required to shine. This is along the same lines as not being worthy. If we pad ourselves with pounds then we are hidden from the world. If we are an overweight or sick person a lot of times not much is expected of us and we can slide by. And for some people that is a great place to be. If no one sees me, they do not see my shame, guilt, unworthiness, or brokenness. This allows me to not step out of my comfort zone and reach goals or expectations.
Although this is just the tip of the iceberg when it comes to emotional issues the good news is they can all be attacked. And when I say attacked I mean it -- emotional baggage is not as easy to lose as real luggage. You can not just check it, because it always makes it to the next airport. Even though you can not lose this baggage you can gladly push it out. It takes work, overcoming emotional baggage is not usually an accident. You need to look at your heart and see where it may be broken. Find someone invested in you, someone who cares, is equipped to deal with the hard stuff, and someone you trust then talk things through with them. Some times just acknowledging and discussing helps you move forward. You can also look into programs such as The Emotion Code or Mind over Medicine.