If HDMI and optical connections are both digital, why does HDMI provides better sound?
Neither of the S/PDIF standards for transferring digital audio, optical and coaxial, can transmit the bandwidth necessary for Blu-ray-specific audio formats like Dolby TrueHD, DTS HD Master Audio and Linear PCM. That requires HDMI.
Blu-ray players send Dolby TrueHD signals down S/PDIF cables as Dolby Digital, DTS HD MA as DTS and 5.1 PCM as two-track stereo PCM. Some players also have an option to convert everything to DTS.
How serious a problem this is is a matter of opinion. Dolby Digital and DTS both use lossy compression, so some sound quality is lost. How much? In my opinion, Dolby Digital can still sound awesome, and DTS sounds even better, but TrueHD and Master Audio beat both of them.
You lose more with 5.1 surround LPCM soundtracks than you do with compressed tracks. Because S/PDIF can't handle that many uncompressed tracks, the player downmixes the signal to two-track stereo (hopefully with Dolby Surround encoding). Luckily, this type of soundtrack is very rare, I've only seen three 5.1 LPCM soundtracks and they were all early Blu-ray releases.
To my knowledge, these high quality, HDMI-requiring soundtracks don't exist in broadcast, cable or satellite TV. You don't lose anything connecting your DVR, satellite or cable box to your receiver with an optical or coax cable.
Find your next job with techworld jobs