We derive and design filter functions of the laser pulses for two-qubit gates on trapped ions, in order to suppress the effects of time-varying fluctuations of the motional-mode frequencies. This improves the experimentally measured gate fidelity from 99.23(7)% to 99.55(7)% in a five-ion chain.