First, note that this is quite a memory inefficient way to do this. You might, instead, consider the answers on this question.
As for your question, though...
D2B=({0..1}{0..1}{0..1}{0..1}{0..1})
This line creates an array called D2B that contains all possible binary values from 00000 to 11111. Brace expansions work like this:
% printf '%s %s %s\n' {0..2}{0..2}{0..2}
000 001 002
010 011 012
020 021 022
100 101 102
110 111 112
120 121 122
200 201 202
210 211 212
220 221 222
Due to the fact that brace expansions are expanded from the first to the last when they are adjoined, the array looks something like this:
00000 00001 00010 .... 11101 11110 11111
The indexes of these values correspond to the binary values contained within them, since here, brace expansions and the mathematic representations of numbers use the same rules.
As such, index 0 is 00000, index 5 is 00101, and so on. Since you pass $val as the index to use, the binary corresponding to your value is echoed.
echo {0..1}{0..1}{0..1}{0..1}{0..1}output?