Resolution of gauge ambiguities in ultrastrong-coupling cavity quantum electrodynamics