This is done automatically when you export to PDF unless you changed TOC parameters.
First condition to get a “correct” TOC in Writer is to style all your chapter, sub-chapter, … headings with the Heading n paragraph style family. Chapter heading is formatted with Heading 1, sub-chapter with Heading 2, etc.
The TOC must not be created manually, otherwise Writer does not know it is a TOC. Use
Indexes & Tables>
Index, TOC or Bibilography. The Heading n paragraphs are collected to make the TOC.
By default, TOC entries will contain hyperlink to jump back to the headings. These links are forwarded to the PDF.
More information on the style system can be found in the Writer Guide.
To show the community your question has been answered, click the ✓ next to the correct answer, and “upvote” by clicking on the ^ arrow of any helpful answers. These are the mechanisms for communicating the quality of the Q&A on this site. Thanks!