Try putting Optifine's jar file into your mods folder (If you don't have one, just make a new file in your '.minecraft' file titled 'mods').
If that doesn't work, try making sure that your Forge, Optifine, and Minecraft version numbers are all the same (1.12, 1.7, etc.).