LibreOffice is a fork of OpenOffice, which itself was based on StarOffice. StarOffice corp, before being acquired by Sun Microsystems, had reverse-engineered rough compatibility with MS Office 97 formats, something that several other companies (e.g. Corel) had done as well.
When MS released OfficeXP, they published the full spec to their quasi-XML format in an attempt to discourage European and regional US governments from switching to either OpenOffice or Adobe products, both of whom had published open formats. This spec publication enabled the contributors to The Document Foundation to greatly improve OpenOffice's MS document conversion (although that required a lot of work and sponsorship).
Since LibreOffice is the fork of OpenOffice belonging to The Document Foundation, it has all of that code.
The above is from my personal recollection; I was a contributor to OpenOffice from version 0.8 to 2.0. Apologies if I've glossed over anyone's important contributions.