The key to understanding how this fits with Paul, I think, is not to say "justified" means something different in James, because it clearly means "being counted righteous" from vs 23. The key is to understand the way in which we are justified by works. Paul so powerfully argues (ex. Rom 3:28, 4:1-5; Gal 2:15-16) that we are not justified by works in the sense that works adding merit to our justification (as if Jesus' merit is not enough, or as if we cannot receive all of it and must supply some of our own). On the other hand, James argues here that we are justified by works, by those works giving evidence of our living/useful faith. They, in fact, are the expression of faith. We should also note that Paul speaks of "faith apart from works" in relation to "works of the law." This, again, is meritorious. If you obey the law, God will bless you. If you disobey, God will curse you. But James is not talking about works of the old covenant law. He has been holding up the "law of freedom" in which mercy triumphs over judgment. And so the works he speaks of is not living in accord to a written code. The examples James gives of "works" within this passage are insightful. Wishing someone warmth and to be filled, while refusing to give them basic food and clothing which you have to give. This "work" shows insincerity in your wish for them. (Note: this is a comparison in the text, and so not directly named a "work," but it seems natural to understand it this way too.) Demons shudder at hearing God is one. Their shuddering here is their "work" and is hugely insightful into what kind of works James has in mind. Namely, any outward expression demonstrating the essence of your faith. Abraham offering up Isaac. This directly related to God's promise to make nations out of Abraham. Abraham's "work" here showed what he believed about God and his ability to even raise the dead. Rahab protecting the messengers. Her "work" showed clearly that she believed that God was with Israel and that her only option was to join that side—for fighting it would be useless! Note that none of these examples have anything to do with old covenant law! Note also that at least half of them are not something for us to emulate directly. All of them simply were visible expressions of an invisible faith. That is how a person is justified by works.
Main point summary
Someone with right thinking shows their faith by their works. In all the Bible, faith without works is demonstrably useless.
Τί τὸ ὄφελος, ἀδελφοί μου,
What good is it, my brothers,
ἐὰν πίστιν λέγῃ τις ἔχειν
if someone says he has faith
ἔργα δὲ μὴ ἔχῃ;
but does not have works?
μὴ δύναται ἡ πίστις σῶσαι αὐτόν;
Can that faith save him?
ἐὰν ἀδελφὸς ἢ ἀδελφὴ γυμνοὶ ὑπάρχωσιν
If a brother or sister is poorly clothed
καὶ λειπόμενοι ὦσιν τῆς ἐφημέρου τροφῆς
and lacking in daily food,
εἴπῃ δέ τις αὐτοῖς ἐξ ὑμῶν•
and one of you says to them,
ὑπάγετε ἐν εἰρήνῃ,
“Go in peace,
θερμαίνεσθε καὶ χορτάζεσθε,
be warmed and filled,”
μὴ δῶτε δὲ αὐτοῖς τὰ ἐπιτήδεια τοῦ σώματος,
without giving them the things needed for the body,
τί τὸ ὄφελος;
what good is that?
οὕτως καὶ ἡ πίστις,
So also faith by itself,
ἐὰν μὴ ἔχῃ ἔργα,
if it does not have works,
νεκρά ἐστιν καθʼ ἑαυτήν.
Ἀλλʼ ἐρεῖ τις•
But [on the other hand] someone will [rightly] say,
σὺ πίστιν ἔχεις,
“You have faith
κἀγὼ ἔργα ἔχω•
and I have works.
δεῖξόν μοι τὴν πίστιν σου χωρὶς τῶν ἔργων,
Show me your faith apart from your works,
κἀγώ σοι δείξω ἐκ τῶν ἔργων μου τὴν πίστιν.
and I will show you my faith by my works.
σὺ πιστεύεις ὅτι εἷς ἐστιν ὁ θεός,
You believe that God is one;
you do well.
καὶ τὰ δαιμόνια πιστεύουσιν
Even the demons believe—
Θέλεις δὲ γνῶναι, ὦ ἄνθρωπε κενέ,
Do you want to be shown, you foolish person,
ὅτι ἡ πίστις χωρὶς τῶν ἔργων ἀργή ἐστιν;
that faith apart from works is useless?
Ἀβραὰμ ὁ πατὴρ ἡμῶν οὐκ ἐξ ἔργων ἐδικαιώθη
Was not Abraham our father justified by works
ἀνενέγκας Ἰσαὰκ τὸν υἱὸν αὐτοῦ ἐπὶ τὸ θυσιαστήριον;
when he offered up his son Isaac on the altar?
βλέπεις ὅτι ἡ πίστις συνήργει τοῖς ἔργοις αὐτοῦ
You see that faith was active along with his works,
καὶ ἐκ τῶν ἔργων ἡ πίστις ἐτελειώθη,
and faith was completed by his works;
καὶ ἐπληρώθη ἡ γραφὴ ἡ λέγουσα•
and the Scripture was fulfilled that says,
ἐπίστευσεν δὲ Ἀβραὰμ τῷ θεῷ,
“Abraham believed God,
καὶ ἐλογίσθη αὐτῷ εἰς δικαιοσύνην
and it was counted to him as righteousness”—
καὶ φίλος θεοῦ ἐκλήθη.
and he was called a friend of God.
ὁρᾶτε ὅτι ἐξ ἔργων δικαιοῦται ἄνθρωπος
You see that a person is justified by works
καὶ οὐκ ἐκ πίστεως μόνον.
and not by faith alone.
ὁμοίως δὲ καὶ Ῥαὰβ ἡ πόρνη οὐκ ἐξ ἔργων ἐδικαιώθη
And in the same way was not also Rahab the prostitute justified by works
ὑποδεξαμένη τοὺς ἀγγέλους
when she received the messengers
καὶ ἑτέρᾳ ὁδῷ ἐκβαλοῦσα;
and sent them out by another way?
ὥσπερ γὰρ τὸ σῶμα χωρὶς πνεύματος νεκρόν ἐστιν,
For as the body apart from the spirit is dead,
οὕτως καὶ ἡ πίστις χωρὶς ἔργων νεκρά ἐστιν.
so also faith apart from works is dead.
Another option: "But someone will say that you..." Another option: quote includes 18b-c.
17a adn 17c are really one proposition.
Shuddering is the demon's works. Demons show what their faith is by their shuddering at God's oneness (absolutely authority?).
"fulfilled" is the key word here, paralleling "completed"
We should note that this was said of Abraham far before Isaac was even born! Thus, "when" in 21b is not temporal, and all these things rest on God knowing the end from the beginning. (Also note that Abraham did not know he was accounted righteous, so far as we can tell.) As for being called the friend of God, this came long after the events.