In order to write a will, sign a contract, get married, or do a variety of other actions, a person must have legal capacity. This means that they must be mentally competent, understand what they're doing and be of a certain age.
Legal capacity often comes into question when someone is signing a will or powers of attorney. In order to have a valid document, the person must have the ability to know what they are signing. Therefore, someone who is not "of sound mind" will lack legal capacity. A person who already has a legal guardian and therefore no legal authority to act on their own lacks legal capacity. A person who is suffering from dementia, certain other medical or psychological disorders may lack legal capacity.
For this reason, an attorney will do his or her best to determine that a client is of sound mind and understands what s/he is doing when s/he is signing a will or power of attorney.
It is important to sign the these types of documents while you still have the capacity and understanding to do so. If you believe that that you may have a problem in the near future do not wait to deal with these issues. Once you lose the ability to act on your own, you will not be able to participate in signing a legally valid document.