If a person was dying from a terminal illness, and treatment offered was unlikely to benefit  (although a remote possiblity that it could help), does the person have to take the treatment?  

As the treatment has many bad side-effects, and the person may not want to suffer them? 

In general, does have a muslim have to take medicine for illnesses, or is it optional?

Praise be to Allaah. 

Generally
speaking, medical treatment or seeking a cure is allowed, because of
the report of Abu’l-Darda’ (may Allaah be pleased with him) who said:
“The Messenger of Allaah (peace
and blessings of Allaah be upon him) said: ‘Allaah has sent down the
disease and the cure, and has made for every disease the cure. So treat
sickness, but do not use anything haraam’” (Reported by Abu Dawood,
3376), and the hadeeth of Usaamah ibn Shurayk (may Allaah be pleased
with him) who said: ‘The Bedouin said, “O Messenger of Allaah, should
we not treat sickness?” He said: “Treat sickness, for Allaah has not
created any disease except He has also created the cure, except for
one disease.” They said, “O Messenger of Allaah, what is it?” He said:
“Old age.”’” (Reported by al-Tirmidhi, 4/383, no. 1961. He said: This
is a saheeh hasan hadeeth. See also Saheeh
al-Jaami’
, 2930).

 The
majority of scholars (Hanafi and Maaliki) said that medical treatment
is mubaah (permitted). 
The Shaafi’is, and al-Qaadi, Ibn ‘Aqeel and Ibn al-Jawzi among the Hanbalis,
said that it is mustahabb (recommended), because of the hadeeth “Allaah has
sent down the disease and the cure, and has made for every disease the
cure. So treat sickness, but do not use anything haraam,” and other
ahaadeeth which contain instructions to seek cures.  They said:
the fact that the Prophet (peace
and blessings of Allaah be upon him) used cupping and other kinds of
treatment indicates that medical treatment is permitted.  For the
Shaafi’is, treatment is mustahabb
when there is no certainty that it will be beneficial, but when treatment
is certain to be beneficial (such as putting a dressing on a wound),
then it is waajib or obligatory
(an example would be blood transfusions in certain cases).

(See Haashiyat
Ibn ‘Aabideen
, 5/215, 249; al-Hidaayah
Takmilat Fath al-Qadeer
, 8/134; al-Fawaakih
al-Dawaani
, 2/440; Rawdah
al-Taalibeen
, 2/96; Kashshaaf
al-Qinaa’
, 2/76; al-Insaaf,
2/463; al-Aadaab al-Shar’iyyah,
2/359ff, Haashiyat al-Jumal,
2/134).

 Concerning
the saheeh ahaadeeth that speak about medical treatment, Ibn al-Qayyim
said: This does not contradict tawaakul
(putting one’s trust in Allaah), just as warding off hunger, thirst,
heat and cold does not contradict tawakkul
The essence of tawaakul is not complete without resorting to the means which
Allaah has set out in order for us to achieve results both according
to His decree (qadr) and
His laws (shar’). 
Not using these means is contrary to tawaakul:
it goes against and undermines the command and wisdom of Allaah, although
the one who neglects the means may think that this makes his tawakkul
stronger. Ignoring the means is a sign of helplessness that goes against
the true essence of tawakkul,
which is that the heart relies on Allaah to bring the slave whatever
will benefit him in this world and the next, and to protect him from
whatever may harm him in this world and the next. But along with this
reliance, it is essential to take the appropriate means, otherwise he
will be going against the wisdom and command of Allah.  Helplessness
should not be taken as a sign of tawaakul,
nor should tawakkul make
a person helpless. 

(Zaad
al-Ma’aad
, 4/15. See al-Mawsoo’ah
al-Fiqhiyyah
, 11/116)

 In
summary, therefore, seeking a treatment or cure is not obligatory according
to the scholars, unless – according to some – it will definitely be
of benefit.  Since in the situation described in the question there
is no certainty that treatment will be of benefit, and indeed it is
likely to cause suffering to the patient, then there is nothing at all
wrong with not giving the treatment.  The patient should not forget
to put his trust in Allaah and seek refuge in Him, for the gates of
Heaven are open to those who call on Allaah.  He may also seek
treatment (ruqya) by reciting
Qur’aan, such as reading al-Faatihah,
al-Falaq and al-Naas over himself.  This will benefit him psychologically
and physically, as well as bringing him reward.  Allaah is the
Healer and there is no healer but He.