Optimal control theory is a versatile tool that presents a route to significantly improving figures of merit for quantum information tasks. We combine it here with the geometric theory for local equivalence classes of two-qubit operations to derive an optimization algorithm that determines the best entangling two-qubit gate for a given physical setting. We demonstrate the power of this approach for trapped polar molecules and neutral atoms.