I wouldn't necessarily say that the phalanx went out of use before the spread of guns. Due to the dominance of heavy cavalry in Western Europe during the Middle Ages, pikes became very popular with many Crowns. As time went on and early muskets were introduced, you'd have formations like the tercio being made which relied on large numbers of pikes being supported by muskets. It isn't until the use of field artillery small enough for anti-infantry use would they fall aside.
The shield wall did not disappear. It persisted in use with the infantry-dominated Germanic armies of the early middle ages. At this point it was the dominant method of warfare, with cavalry and archers playing a much inferior role.
By about the 11th century, we begin to see clear evidence of the use of combined arms warfare in western Europe, with the Normans being perhaps its finest practitioners. Their armies generally consisted of a core of solid infantry, probably recruited from the free peasantry, equipped with large shields, spears, and a motley collection of armor, who fought in the shield wall and served as blocking troops. Where it differed from earlier is in the presence of significant numbers of heavy cavalry and archers, employed in concert with the infantry.
This is perhaps best illustrated at the Battle of Hastings, where the ~8,000 or so strong Norman force consisted of something less than 2,000 cavalry, an undetermined number of archers, and the remainder of heavy infantry. The archers opened the battle, but were ineffective. The infantry closed with the Saxon shieldwall, but were unable to break it. William committed his cavalry next, and though initially unsuccessful in their attack, a feigned retreat (some would argue an unplanned repulse) lured some of the Saxon infantry out of their shield wall, and a counter-charge cut many of them down in the open. These losses, combined with fatigue and, possibly, attritional losses from the archery, weakened the Saxon shield wall sufficiently that the Norman horse was able to break it and drive them from the field.
By the 12th century this method of war was ubiquitous in the Frankish world, and was exported to the Middle East via the Crusades. Indeed, Richard I at Arsuf illustrated the critical importance of this triad of combined arms. Though attacked on the march by a Muslim force superior to his own in cavalry, his armored infantry were able to form a bristling screen between the Muslim light horse and his knights, while the crossbowmen kept them from coming close enough to seriously harm the infantry with their bows and javelins. After some hours of this harassment, a fraction of Richard's knights (Templars, I believe) spontaneously charged, and he rolled with it, sending the bulk of his cavalry after them. The Muslim horse, wearied from hours of skirmishing and lacking comparable heavy infantry to shelter behind, were driven from the field by Richard's fresh heavy cavalry.
I wouldn't group the phalanx and the shield wall together, as the phalanx is a very specific manouvre compared to the shield wall which is much more of a general term. The phalanx was at its height during the early hellenistic period, with the improvements made to the length of spears and the support of elite cavalry. These changes were made by Philip II of Macedon if I'm correct in remembering, and allowed him and his son to propel themselves into greatness.
However, the rise of Rome two centuries later heralds the development of more adaptable infantry, though still with the same discipline. Whereas the phalanx relied upon other units to make up for its lack of mobility and weakness at the sides and rear, Roman infantry was both well equipped to deal with spears, (having gained a lot of experience against Carthaginian sacred band) and much more able to firstly, overcome the supporting units of the phalanx, and then exploit the lumbering formation.
This is shown during The Second Macedonian war, in which Titus Flaminius was able to overcome the Macedonians (with some difficulty) and declare freedom of the Greeks, followed by two further wars, in which Rome eventually annexed the region.
In terms of the shieldwall, i couldn't tell you of its downfall, but during the English succession crisis at both the battle of Stamford Bridge and the battle of Hastings it was in use. Both times it was the tactic employed by the losing side, but this shows its key importance to warfare of the period, over 1200 years later.