means that
it is sometimes wise to withdraw from a favourable situation before circumstances deteriorate or an opportunity to leave disappears.
When you should leave, escape, or extricate from some place or situation while you have the chance, before conditions worsen and becomes impossible…, the saying “Get out while the going is good” is appropriate.