You may have the wrong keyboard. Apple Store gave me a wrong keyboard with my iPad and I luckily realized this problem on the way out and came back and returned it. It did this exact thing you describe.
I had the 9.7 smart keyboard and it always laid flat, but I have a friend whose 9.7 always left a gap with the table. Then I bought the 10.5 smart keyboard for my new 10.5 ipad and the keyboard wouldn't lie flat. I could always bend the joint and make it lie flat, but I'd have to do that every time. I took it back after a week and swapped it out. The new one lies flat every time. So they are hit or miss, but you can get one that lies flat every time.