Google's John Mueller said in a Hangout that it is better to have a website with valid HTML:
“This happened recently with the change of guidelines, in regards to the changes that were made to the webmaster guidelines. We mentioned 'use valid HTML'. The question here is: Is the W3C validator (broken HTML) ranking factor or should we not care about that?
It is not directly a ranking factor. It's not that if your site is not using valid HTML we are going to remove it from the index. Because I think we're going to have empty search results spaces.
However, there are some aspects that come into play there. On the one hand, a site with really broken HTML, something we see very rarely, is very difficult for us to crawl and index to the content because we can't find it.
The other two aspects that are one more kind of structured data. Sometimes it is very difficult to collect structured data when the HTML has been completely broken. Therefore you cannot easily use a validator for structured data.
The other thing is when it comes to mobile devices and cross-browser support is if you have broken HTML, then this is sometimes very difficult to do on newer devices.”
This video shows a more detailed answer:
In short:
There is no penalty for broken HTML code. Google can handle a lot of HTML errors. However, some HTML errors make it difficult for Google to index web pages.
Other HTML errors can cause problems with structured data markup, other errors can cause display problems on mobile devices.
For that reason, it is much better to ensure that your web pages use valid HTML. Valid HTML is a sign of a website is high quality.