How can somebody best ensure that when they enter into an agreement or relationship with somebody, that they won't later be able to complain that they were unable to understand the terms by reason of either some mental impairment or some disability or deficiency in learning?
An adult is normally assumed to be competent to enter into a contract unless there is some reason to think otherwise. It is not us\ual to demand evidence of competency unless ther is something in the appearance or actions of a party which raises such a question, or something about that party, such as a history of mental illness, known to the other party, which raises such a question.
I have heard of a party being medically examined just before signing an important document, to provide evidence of competence, but generall in connection with a will, and generally when the signer is elderly and in poor physical condition, and even then such a procedure is rare.
If competence is disputed, medical evidence is required to resolve the question, or more exactly to establish lack of competence. In the absence of such evidence, competence will be presumed. That was the conclusion of the MA Supreme Court in FRANCES M. SPARROW vs. DAVID D. DEMONICO & another. The court held that "without medical evidence or expert testimony that the mental condition interfered with the party's understanding of the transaction, or her ability to act reasonably in relation to it, the evidence will not be sufficient to support a conclusion of incapacity. " It was a significant part of the court's reasoning in that case that a reasonable person might well have entered into the agreement in question, and that the party was represented by a lawyer, and acted in accord with the lawyer's advice. That does not, however, seem to be a requirement, merely additional evidence of the reasonableness of the action. 461 Mass. 322 (2011) (this case was linked in a comment above by @user3851)