Somnatha Jyotirlinga is first of the Jyotirlingas on Earth (Bhooloka). Story of Somesvara is described in detail in both Shiva Mahapurana and Skanda Maha Purana.
This is how Chapter 14 of RudraSamhita of Shiva Purana describes the story.
Daksha's 27 daughters were married to Soma, the moon God. But Moon God always loved Rohini more compared to other wives. This made other wives infuriated and they explained their tale of suffering to their father Daksha.
सप्तविंशन्मिताः कन्या दक्षेण च महात्मना।
तेन चन्द्रमसे दत्ता
अश्विन्याद्या मुनीश्वराः॥ ५॥
O great sage, his (Daksha) daughters,
twenty-seven in number, Ašvini and others, were given in marriage to
the moon by Dakša, the great soul.
चन्द्रं च स्वामिनं प्राप्य शोभमाना विशेषतः।
चन्द्रोऽपि चैव ताः
प्राप्य शोभते स्म निरन्तरम्।। ६ ।।
Achieving the moon as their
lord, all the girls felt graceful. The moon also felt graceful with
them.
हेम्ना चैव मणिर्भाति मणिना हेम चैव हि।
एवं च समये तस्य यञ्जातं
श्रूयतामिति॥७॥
The gold looks graceful with the gem and same is
the case of gems with the gold. Now you listen to what happened
thereafter.
सर्वास्वपि च पत्रीषु रोहिणीनाम या स्मृता।
यथैका सा प्रिया
चासीत्तथाऽन्या न कदाचन॥८॥
Of all the twenty-seven wives, he did
not love any one so much at anytime as he loved Rohiņī.
अन्याश्च दुःखमापन्नाः पितरं शरणं ययुः।
गत्वा तस्मै च यद्दुःखं तथा
ताभिर्निवेदितम्॥९॥
The rest of the wives of the moon went to
their father, feeling painful and took refuge with him. They explained
the tale of their sufferings to their father.
Daskha tried to convince Soma, but moon God didn't heed. Daksha cursed the Soma to get consumed by disease (waning).
दक्षश्चैवं स सम्प्रार्थ्य चन्द्रं जामातरं स्वयम्।
जगाम मन्दिरं
स्वं वै निश्चयं परमं गतः।। १३॥
Sūta said, “Thus requesting to moon, his son-in-law, Daksa returned to
his place feeling satisfied.
चन्द्रोऽपि वचनं तस्य न चकार विमोहितः।
शिवमायाप्रभावेण यया
सम्मोहितं जगत्॥ १४॥
The moon on the other hand, having been over-powered with the delusion
of Siva,did not bother about the advice of Daksa.
श्रूयतां चन्द्र! यत्पूर्व प्रार्थितो बहुधा मया।
न मानितं त्वया
यस्मात्तस्मात्त्वं च क्षयी भव।। १८ ।।
Daksa said, “O Moon, you listen, I have made several requests to you
which remain unheaded. Therefore you will suffer from the disease of
consumption.”
सूत उवाच
इत्युक्त तेन चन्द्रो वै क्षयी जातः क्षणादिह।
हाहाकारो महानासीत्तदेन्दौ क्षीणतां गते॥ १९॥
Sūta said, “At these words of Daksa, the moon atonce attracted the
consumption disease, with the moon’s attracting the ailment of
consumption, there was a turmoil everywhere.
All the sages with Devas being unsettled by changes went to Prabhasa Khanda and said to Soma to worship Shiva with Mahamrutyunja mantra.
स्थाप्य चन्द्रं प्रभासे च स्वं स्वं धाम ययुर्मुदा॥३८॥
Invoking all the holy places there, including Sarasvati, they started
adoring Siva by Mrtyuñjaya-pujan-vidhi. The gods and the ascetics,
with the spotless intentions, seated the moon at the Prabhāsa-kSetra,
and went back to their respective places.
चन्द्रेण च तपस्तमं षण्मासं च निरन्तरम्।
मृत्युञ्जयेन मन्त्रेण
पूजितो वृषभध्वजः।। ३९ ।।
The moon performed the adoration of Siva reciting the
Mrtyuñjaya-mantra and performed tapas continuously for six months.
Pleased with his penance, Shiva appeared to Soma and asked for a boon. Soma asked to forgive him and cure from that disease of consumption. Shiva said he would go through period of waning and waxing for period of fortnight continuously.
शिव उवाच
पक्षे च क्षीयतां चन्द्र! कला ते च दिने दिने।
पुनश्च
वर्द्धतां पक्षे सा कला च निरन्तरम्।। ४५ ।।
Śiva said, “In a fortnight, you will get reduced by a digit daily and
in the next fortnight, it would go on increasing in the same
sequence.”
Pleased with devotion of moon and at request of Devas, Shiva then took form of Linga there as Somnathesvara.
चन्द्रस्य यशसे तत्र नाम्ना चन्द्रस्य शङ्करः।। ५०॥
सोमेश्वरश्च
नाम्नाऽऽरीद्विख्यातो भुवनत्रये।
क्षयकुष्ठादिरोगाणां नाशक:
पूजनाद्द्वजाः।। ५१।।
Siva then felt happy with the gods. In order to
enhance the glory and greatness of the region, besides that of the
moon, Šiva established himself there in the form of Someşvara and
became famous in the three worlds. O Brähmanas, by his worship, Šiva
becomes the destroyer of the ailments like consumption, leprosy and
others.
Story of Somnatha is also described in Chapter 63 Nagara Khanda of Skanda Purana (Pg: 259) and Chapter 22 and 23 Section one of Prabhasa Kandha of Skanda Purana.
Greatness of Somnatha Linga is described in much detail in Chapter 6 Section one of Prabhasa Kandha of Skanda Purana.