How did Elijah know he was going to be taken up to heaven? How did Elijah know that he was about to be taken by God?
How did Elisha know this was going to happen?
I could not find anything prior to 2 Kings 2:1 when Scripture states that Elijah was going to be taken up, and 2:9 when Elijah asks what he can do for Elisha before he is taken.
Scripture does not say. The closest thing we get is this:


  and Jehu the son of Nimshi you shall anoint king over Israel; and Elisha the son of Shaphat of Abel-meholah you shall anoint as prophet in your place. —1 Kings 19:16 NASB


God tells Elijah to anoint Elisha in his place, implying that it would not be long before Elijah was finished with his time as God's prophet. I found this commentary that mentions Elijah knew beforehand, but I could not find a Scriptural reference.