One approach is to use a stack, where each entry in the stack is a value:index pair. Iterate through the input array, popping items from the stack whose value is less than the value of the current item in the input array. Once all of the smaller values have been popped from the stack, push the current value:index pair onto the stack. When the end of the input array is reached, any remaining entries in the stack get an output value of -1 to indicate that no larger number was found.
Using the example in the question, here's how the algorithm would work
input item 12
stack = 12:0
input item 15
pop 12:0 and set output[0] = 15
stack = 15:1
input item 22
pop 15:1 and set output[1] = 22
stack = 22:2
input item 9
stack = 9:3, 22:2
input item 7
stack = 7:4, 9:3, 22:2
input item 2
stack = 2:5, 7:4, 9:3, 22:2
input item 18
pop 2:5 and set output[5] = 18
pop 7:4 and set output[4] = 18
pop 9:3 and set output[3] = 18
stack = 18:6, 22:2
input item 23
pop 18:6 and set output[6] = 23
pop 22:2 and set output[2] = 23
stack = 23:7
input item 27
pop 23:7 set output[7]= 27
stack = 27:8
end of array
pop 27:8 and set output[8] = -1
done