Video source: https://youtu.be/EsZMjhOOWnE
First of all get the latest version of OptiFine from Here
To install OptiFine, you can either install it as a standalone using the JAR file as the installer. Read Here for more information on getting Java setup and how to open JAR files.
You can also use OptiFine as a mod.
To use with Forge, get Forge from Here (make sure you get the correct version of Forge as shown on the OptiFine website)
Place the OptiFine jar file inside the Mods folder and you can use it as a regular Forge Mod.
You can also use OptiFine with Fabric. Grab OptiFabric from Here
Place both OptiFabric and OptiFine jar files inside the mods folder and you can use OptiFine with Fabric now.
For the best performance settings watch the video from the top link.
For all of the OptiFine optimal settings, watch this video - https://youtu.be/CCR-J-krcV8