There have been several threads on the forum that have covered this topic over the years.
To cut to the chase, you first need to determine if the problem is mechanical or electrical.
1) Disconnect the coupler from the board in the tender. Remember which socket it came out of because you have two choices in that tender. Operate the locomotive/tender as you normally would and if the coupler still opens, the issue is likely mechanical. You can either fiddle with the internal coupler spring or replace the coupler.
2) If the coupler holds tight during operation when disconnected from the board, then the issue is likely electrical. A 1uf (microfarad), 50v non-polarized/bi-polar capacitor across the coupler socket terminals on the board will usually fix the issue.
Note that adding the cap is a solution for TMCC and early Legacy era locomotives only. It is NOT a solution for locomotives with the Legacy RCMC board.