Vishnu has no beginning or end. As Krishna says in the Bhagavad Gita, "Neither the gods nor the great Rishis know any birth of Me, for I am altogether and in every way the origin of the gods and the great Rishis. He who knows Me as the unborn, as the beginningless, as the Supreme Lord of all the worlds – he only, undeluded among men, is freed from all sins."
Brahma was born from a lotus flower that emerged from Vishnu's navel, as described in the Srimad Bhagavatam:
Garbhodakaśāyī Viṣṇu was alone, lying on His bedstead, the great snake Ananta, and although He appeared to be in slumber in His own internal potency, free from the action of the external energy, His eyes were not completely closed.... The Lord lay down for a thousand cycles of four yugas in His internal potency, and by His external energy He appeared to be sleeping within the water.... The subtle subject matter of creation, on which the Lord’s attention was fixed, was agitated by the material mode of passion, and thus the subtle form of creation pierced through His abdomen. Piercing through, this sum total form of the fruitive activity of the living entities took the shape of the bud of a lotus flower generated from the Personality of Viṣṇu.... Into that universal lotus flower Lord Viṣṇu personally entered as the Supersoul, and when it was thus impregnated with all the modes of material nature, the personality of Vedic wisdom, whom we call the self-born, was generated. Brahmā [was] born out of the lotus flower.
The Srimad Bhagavatam also describes how Shiva was born out of Brahma's anger:
There was much anger generated in the mind of Brahmā, which he tried to control and not express. Although he tried to curb his anger, it came out from between his eyebrows, and a child mixed blue and red was immediately generated. After his birth he began to cry: O destiny maker, teacher of the universe, kindly designate my name and place. The all-powerful Brahmā, who was born from the lotus flower, pacified the boy with gentle words, accepting his request, and said: Do not cry. I shall certainly do as you desire. Thereafter Brahmā said: O chief of the demigods, you shall be called by the name Rudra by all people because you have so anxiously cried. My dear boy, I have already selected the following places for your residence: the heart, the senses, the air of life, the sky, the air, the fire, the water, the earth, the sun, the moon and austerity.... My dear boy Rudra, you have eleven other names: Manyu, Manu, Mahinasa, Mahān, Śiva, Ṛtadhvaja, Ugraretā, Bhava, Kāla, Vāmadeva and Dhṛtavrata.
This story is presented in longer form in the Shatapatha Brahmana of the Yajur Veda:
[Brahma] said to him, 'My boy, why criest thou, when thou art born out of labour and trouble?' He said, 'Nay, but I am not freed from (guarded against) evil; I have no name given me: give me a name!' Hence one should give a name to the boy that is born, for thereby one frees him from evil;--even a second, even a third (name), for thereby one frees him from evil time after time.
He said to him, 'Thou art Rudra' And because he gave him that name, Agni became suchlike (or, that form), for Rudra is Agni: because he cried (rud) therefore he is Rudra. He said, 'Surely, I am mightier than that: give me yet a name!'
He said to him, 'Thou art Sarva.' And because he gave the him that name, the waters became suchlike, for Sarva is the waters, inasmuch as from the water everything (sarva) here is produced. He said, 'Surely, I am mightier than that: give me yet a name!'
He said to him, 'Thou art Pasupati.' And because he gave him that name, the plants became suchlike, for Pasupati is the plants: hence when cattle (pasu) get plants, then they play the master 3 (patîy). He said, 'Surely, I am mightier than that: give me yet a name!'
He said to him, 'Thou art Ugra.' And because he gave him that name, Vâyu (the wind) became suchlike, for Ugra is Vâyu: hence when it blows strongly, they say 'Ugra is blowing.' He said, 'Surely, I am mightier than that: give me yet a name!'
He said to him, 'Thou art Asani.' And because he gave him that name, the lightning became suchlike, for Asani is the lightning: hence they say of him whom the lightning strikes, 'Asani has smitten him.' He said, 'Surely, I am mightier than that: give me yet a name!'
He said to him, 'Thou art Bhava.' And because he gave him that name, Parganya (the rain-god) became suchlike; for Bhava is Parganya, since everything here comes (bhavati) from the rain-cloud. He said, 'Surely, I am mightier than that: give me yet a name!'.
He said to him, 'Thou art Mahân Devah (the Great God).' And because he gave him that name, the moon became suchlike, for the moon is Pragâpati, and Pragâpati is the Great God. He said, 'Surely, I am mightier than that: give me yet a name!'
He said to him, 'Thou art Îsâna (the Ruler).' And because he gave him that name, the Sun became suchlike, for Îsâna is the Sun, since the Sun rules over this All. He said, 'So great indeed I am: give me no other name after that!'
It should be noted that even though Shiva is the son of Brahma and Brahma is the son of Vishnu, Shiva is still in some sense also a direct creation of Vishnu. As Krishna says in the Shanti Parva of the Mahabharata, "The ancestors of civilization, and entities such as Shiva, were created by Me. They do not know of this because they are illusioned by My cosmic material energies."