The correct answer is D. to.
The word “adhere” means to stick or hold fast to something. In the context of the sentence, “Both parties must adhere to the terms of the contract,” it means that both parties must follow the rules and conditions of the contract.
The other options do not make sense in the context of the sentence. The word “in” is used to indicate a location or position. The word “by” is used to indicate a means or method. The word “on” is used to indicate a surface or location.
Therefore, the correct answer is D. to.