Not sure about the weight gain, but I can answer as to its function.
Your body burns sugar all the time, even when you're asleep. During the day the sugar generally comes from what you eat (and you can inject a bolus short acting insulin to counteract that), but when you're fasting or at night then there's a (reasonably) steady supply from your liver, which also needs insulin. That's what your basal (lantus) is for.
As a T2, your body isn't processing sugar very well, and isn't producing enough (any?) insulin to do that processing. The basal is supplementing your own night time insulin production so that your blood sugar levels stay in the correct range.