Since imamate is a divine rank and post, determining who is to be imam and their number is in Allah’s (swt) hands, and the only way to learn of these matters, is through his prophet (pbuh). His Excellency has mentioned their numbers and who they are in many different incidents and cases. One of those cases is a hadith that great Sunni scholars have narrated. They have reported the Prophet (pbuh) saying: “Islam’s job will not be done until twelve successors come.” Also, both Shia and Sunni scholars have narrated that after his final hajj, in Ghadir Khum, when the Prophet (pbuh) appointed Ali (as) as his successor and the verse of “The completion of religion” was revealed as a result, the first and second caliphs stood up and asked the Prophet (pbuh): “Is this successorship and authority exclusively for Ali after you?” His Excellency replied: “It exclusively belongs to Ali (as) and my successors till the Day of Judgment. They went on to ask: “Who are your successors?” His Excellency replied: “Ali is my brother, minister, inheritor, successor and khalifah of my people and has authority over all believers after me, after him, my son Hasan and after him, my son Husein and after them nine individuals of the progeny of Husein that will take each other’s place one after another.”
Therefore, the main reason for why the imams are twelve individuals are the many hadiths that both the Shia and Sunnis have narrated from the Prophet (pbuh). As for the philosophy behind their number being twelve one must say that there are numerous issues in Islam that we don’t know the philosophy of, like the number of the rak’ats of prayers, or like the loud recitation of prayer for men and the opposite for women. This question goes under that category. According to hadiths, these blessed individuals had been created before their creation and coming to this world and the Prophet (pbuh) was aware of them and his Excellency had announced their successorship beforehand. Anyways, all of Islam’s guidelines, regardless of whether we know their philosophies or not, need to followed and executed, because what we are sure about is that all of them have been legislated by the All-Wise and that the He never legislates something that is not to the benefit of those he is legislating for. Hence, all of Islam’s guidelines are for our own good and for us to reach perfection and need to be carried out.
And now the second part of your question which asks about why we have to be alone for years and how we can reach salvation during the absence of our imam.
The answer to your question is that Allah (swt) never leaves the world without His hujjah or “proof” (refers to the complete individual, let it be an imam or prophet). This is what our hadiths teach us, thus one can't say that we are completely alone today.
One of the characteristics of the hujjahs of Allah (swt) is that they are the mediators of His grace and mercy to us, meaning that it is because of them that Allah (swt) bestows His grace and mercy upon us. This matter can be learned from a hadith of the Prophet (pbuh) that says that the grace of Imam Zaman (as) reaches the servants of Allah (swt) during his absence the same way that the sun’s heat reaches the earth from behind the clouds. The nice proverb that has been stated in this hadith gives us many facts, one of them being that Imam Zaman’s absence is like that of the sun behind the clouds. Although it can no longer be seen, we aren’t deprived of its other benefits. The same goes for Imam Zaman’s absence, although he can no longer be seen, yet this doesn’t result in us not making use of his other benefits such as his guidance and grace, and that is why many have been benefited by him in history in which their stories have been recorded. This subject has also been asserted in his Excellency’s letter to Sheikh Mufid where his Excellency says: “We never forget you and there is no shortcoming on our part regarding your affairs, and if it wasn’t so, the storm of dangers and catastrophes would have abolished you.” Also, his presence (although he is not seen) is an important factor of giving hope to the Muslim nation in order for them to fix themselves and be prepared for his Excellency’s reappearance.
The only important point that remains is that one can make better use of the sun’s light when he/she pushes all of the different obstacles aside, therefore one needs to try to remove as many obstacles between him/her and the imam (as) as possible in order to draw ever nearer to him and make better use of his presence.
Conclusion: When we have such a big sun (even behind the clouds) there is no need to feel alone and worry.
For further information on the benfits of Imam Zaman (as), see questions: 168 (website: 1375), 654 (website: 705) and 534 (website: 582).
 حَدَّثَنَا قُتَیْبَةُ بْنُ سَعِیدٍ حَدَّثَنَا جَرِیرٌ عَنْ حُصَیْنٍ عَنْ جَابِرِ بْنِ سَمُرَةَ قَالَ سَمِعْتُ النَّبِیَّ صَلَّى اللَّهُ عَلَیْهِ وَسَلَّمَ یَقُولُ ح و حَدَّثَنَا رِفَاعَةُ بْنُ الْهَیْثَمِ الْوَاسِطِیُّ وَاللَّفْظُ لَهُ حَدَّثَنَا خَالِدٌ یَعْنِی ابْنَ عَبْدِ اللَّهِ الطَّحَّانَ عَنْ حُصَیْنٍ عَنْ جَابِرِ بْنِ سَمُرَةَ قَالَ دَخَلْتُ مَعَ أَبِی عَلَى النَّبِیِّ صَلَّى اللَّهُ عَلَیْهِ وَسَلَّمَ فَسَمِعْتُهُ یَقُولُ إِنَّ هَذَا الْأَمْرَ لَا یَنْقَضِی حَتَّى یَمْضِیَ فِیهِمْ اثْنَا عَشَرَ خَلِیفَةً قَالَ ثُمَّ تَکَلَّمَ بِکَلَامٍ خَفِیَ عَلَیَّ قَالَ فَقُلْتُ لِأَبِی مَا قَالَ قَالَ کُلُّهُمْ مِنْ قُرَیْشٍ
This hadith has been mentioned in many different books such as: Sahih Muslim, vol. 9, pp. 333,335 and 337; Sunan Abi Dawud, vol. 11, pp. 351 and 352; Musnad Ahmad, vol. 42, pg. 309; Musannaf Ibn Abi Shaybah, vol. 7, pg. 492.
 Ghayatul-Maram, chapter 58, hadith 4. In another hadith also under the first verse of surah Nisa’ he has mentioned all of his successors one by one. Ghayatul-Maram, vol. 10, pg. 267; Ithbatul-Hudat, vol. 8, pg. 133; Biharul-Anwar, vol. 36, pg. 260 and 298.
 الْحُسَیْنُ بْنُ مُحَمَّدٍ عَنْ مُعَلَّى بْنِ مُحَمَّدٍ عَنْ بَعْضِ أَصْحَابِنَا عَنْ أَبِی عَلِیِّ بْنِ رَاشِدٍ قَالَ قَالَ أَبُو الْحَسَنِ ع إِنَّ الْأَرْضَ لَا تَخْلُو مِنْ حُجَّةٍ وَ أَنَا وَ اللَّهِ ذَلِکَ .الْحُجَّةُ Usulul-Kafi, vol. 1, pg. 178.
 Biharul-Anwar, vol. 53, pg. 774.