Within its original context in the book of Jeremiah Jer. 31:15 is reflecting on the sorrow of the Babylonian exile. Ramah was a town about 5 miles north of Jerusalem that lay along the route that the exiles were forced to travel between Jerusalem and Babylon (Jer. 40:1). Rachel was buried near Bethlehem (Gen 35:19–20) and is poetically described in Jer. 31:15 as weeping over the loss and suffering of her descendants. Remarkably, Jeremiah 31 as a whole is not focused on sorrow but upon the joy and happiness that will fill God’s people during his future restoration and salvation. The sorrow of exile is remembered by way of contrast to the joy of the coming restoration and healing.
Matthew’s point seems to be that just as Israel went into exile in Babylon in a painful process that involved suffering and loss but would eventually result in the restoration of the entire nation, Jesus went into exile in Egypt in a painful process that again brought suffering and loss to Rachel’s descendants but would lead to the complete fulfillment of the restoration, healing, and salvation prophesied by Jeremiah in Jer. 31.
"I have loved you with an everlasting love; therefore I have continued my faithfulness to you. Again I will build you, and you shall be built . . . for I am a father to Israel, and Ephraim is my firstborn. . . . say 'He who scattered Israel will gather him, and will keep him as a shepherd keeps his flock.' For the Lord has ransomed Jacob and has redeemed him from hands too strong for him. They shall come and sing aloud on the heights of Zion, and they shall be radiant over the goodness of the Lord . . . I will turn their mourning into joy; I will comfort them, and give them gladness for sorrow. I will feast the soul of the priests with abundance, and my people shall be satisfied with my goodness, declares the Lord. . . . There is hope for your future, declares the Lord. . . . For I will satisfy the weary soul, and every languishing soul I will replenish" (Jer. 31:3b-4a, 9c, 10b-12a, 13b-14, 17a, 25).
Even more significantly, Jer. 31 concludes with God's promise of the New Covenant (Jer. 31:31-40).