EEA/EFTA foreign nationals must apply for their national ID number in person at Registers Iceland or some police stations. For other foreign nationals, the national ID number is obtained as part of the residence permit application through the Directorate of Immigration.
Further information is on the Registers Iceland website.