Given a pile of cards, how to program to get card combinations whose sum is closest to 21?
You have at least two options.

If the number of cards is very small, as in your example, then you can go over all possible subsets of cards and compute their sum, taking note of the one closest to 21. Then go over all of them again and output the ones achieving the best sum. With a realistic number of cards this will be very fast.

What they might have expected you to answer is to use dynamic programming. For each sum from 2 to 21, go over all cards and see if it is achievable from an earlier sum (including 0), and if so, annotate this with all possible cards. Now find the best sum and follow the edges back to 0.