You must have your ticket before you board the train, so a few minutes in advance is enough. In order to sit together just choose seats that are together.
Trains in Switzerland are mass transit. Train tickets do not come with assigned seats, and on most trains you cannot reserve seats at all. You just board, find yourself seats and sit down. Buying a month in advance does not affect your probability of being together on the train, as everyone board together, regardless of when you bought the ticket…
On Milano to Lugano you have the choice of two services however: The EC services do come with reserved seats, and tickets for that train are cheaper bought in advance. The RE80 service however does not have reserved seats, and tickets are always the same, so you can just buy that at the station.
Btw: Are you coming from Milano itself, or from the airport? There are direct trains from Malpensa to Lugano.