The best way to connect a soundbar to your TV is with an HDMI cable. You'll need to make sure you have an HDMI port on your soundbar and an HDMI ARC port on your TV. This allows you to use a cable to connect them.
An HDMI port means that the highest quality audio can pass from your TV to your soundbar, like Dolby Atmos.
If your TV or soundbar doesn't have HDMI ports, the second best option is an optical. This can still transfer digital sounds, but not the high-resolution ones you'd get with HDMI.
However, you will still get quality audio and an optical connection does work with soundbars and surround sound systems.